perf: optimize rx-storage-helper.ts write path#8162
Merged
Conversation
- Eliminate redundant stripAttachmentsDataFromRow() call in update path (updatedRow was already stripped, then stripped again when pushed) - Cache and reuse stripped documents for event creation instead of calling stripAttachmentsDataFromDocument() again - Replace Object.entries().forEach() and Object.entries().find() with indexed for loops using Object.keys() to avoid callback overhead - Optimize flatCloneDocWithMeta() to use spread syntax instead of Object.assign + flatClone (fewer intermediate objects) - Simplify insert path by removing redundant branches and using documentDeleted directly instead of ternary conversion - Merge duplicate hasAttachments branches in insert/update paths Performance results (node:memory, 40 runs): - insert-documents-500: 0.24 → 0.21 ms (-12.5%) - serial-inserts-50: 1.35 → 1.24 ms (-8.1%) - find-by-query-parallel-4: 2.79 → 2.55 ms (-8.6%) - serial-find-by-id-50: 0.41 → 0.38 ms (-7.3%) - time-to-first-insert: 0.93 → 0.88 ms (-5.4%) Co-authored-by: pubkey <8926560+pubkey@users.noreply.github.com> Agent-Logs-Url: https://github.com/pubkey/rxdb/sessions/9212f597-676d-4a25-91da-c55b55fedd77
Co-authored-by: pubkey <8926560+pubkey@users.noreply.github.com> Agent-Logs-Url: https://github.com/pubkey/rxdb/sessions/9212f597-676d-4a25-91da-c55b55fedd77
Copilot created this pull request from a session on behalf of
pubkey
March 23, 2026 14:09
View session
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Targeted optimizations to
categorizeBulkWriteRows()and related helpers on the hot write path.Results (node:memory, 40 runs)
Read-path metrics unchanged (within noise).
Changes
stripAttachmentsDataFromRow—updatedRowwas already stripped on creation, then stripped again when pushed tobulkUpdateDocscategorizeBulkWriteRowscalledstripAttachmentsDataFromDocument()separately for event creation despite having the stripped doc frominsertedRow/updatedRowalready availableObject.entries().forEach()/.find()with indexedObject.keys()loops in all attachment iteration paths — eliminates intermediate array + callback overheadflatCloneDocWithMeta:{ ...doc, _meta: { ...doc._meta } }instead ofObject.assign({}, doc, { _meta: flatClone(doc._meta) })— one fewer intermediate objectstripAttachmentsDataFromDocument: sameObject.keys()loop treatmentinsertedIsDeletedternary indirection, merge duplicatehasAttachmentsbranchesBefore (update path stripped twice):
After: