Memoize shallowEqual verdicts by object identity in useOnyx#810
Merged
youssef-lr merged 4 commits intoJul 22, 2026
Merged
Conversation
TMisiukiewicz
force-pushed
the
perf/memoize-useonyx-shallowequal
branch
from
July 21, 2026 10:23
9548cf0 to
94767fa
Compare
fabioh8010
reviewed
Jul 21, 2026
53 tasks
fabioh8010
reviewed
Jul 22, 2026
fabioh8010
approved these changes
Jul 22, 2026
Contributor
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.
Details
Problem
When a large single Onyx key updates (e.g.
personalDetailsListwith ~18k members), every subscribeduseOnyxhook independently runsshallowEqual(previousValue, newValue)insidegetSnapshot()to decide whether to re-render. All of these hooks are comparing the exact same two object references (the old and new cache-owned values), yet each pays for its own full O(keys) walk.Measured on a large account with ~200 subscribers to personalDetailsList: a single update triggered 29 full walks, ~512,000 key comparisons, ~530ms of synchronously blocked JS thread.
Fix
Memoize
shallowEqualverdicts by the identity of the compared object pair:WeakMap>The first hook comparing a given
(prev, next)pair pays for the realshallowEqualwalk and stores the verdict; every other hook comparing the same pair resolves in O(1). Non-object inputs bypass the memo (they're O(1) to compare anyway).Why this is safe
shallowEquallegitimately returns true on different references (fresh collection snapshots with unchanged members) behave identically.WeakMap).Results
personalDetailsListupdateScope note
This change removes the redundancy (N hooks paying for N identical comparisons), not the existence of the O(members) comparison — one full walk per update remains, and it is inherent to
personalDetailsListbeing a single monolithic key. The root-cause fix is migrating it to a proper collection (personalDetails_), so an update only ever touches the changed member's subscribers. That is a large App/backend migration and is intentionally out of scope here; this optimization stands on its own regardless, since it applies to every large key with multiple subscribers (report_,policy_, snapshots, derived keys), not just this one.Related Issues
$ Expensify/App#96856
Linked E/App PR
Expensify/App#96620
Automated Tests
Manual Tests
Author Checklist
### Related Issuessection aboveTestssectiontoggleReportand notonIconClick)myBool && <MyComponent />.STYLE.md) were followedAvatar, I verified the components usingAvatarare working as expected)/** comment above it */thisproperly so there are no scoping issues (i.e. foronClick={this.submit}the methodthis.submitshould be bound tothisin the constructor)thisare necessary to be bound (i.e. avoidthis.submit = this.submit.bind(this);ifthis.submitis never passed to a component event handler likeonClick)Avataris modified, I verified thatAvataris working as expected in all cases)mainbranch was merged into this PR after a review, I tested again and verified the outcome was still expected according to theTeststeps.Screenshots/Videos
Android: Native
Android: mWeb Chrome
iOS: Native
iOS: mWeb Safari
MacOS: Chrome / Safari
Screen.Recording.2026-07-22.at.08.57.35.mov