-
Notifications
You must be signed in to change notification settings - Fork 96
Memoize shallowEqual verdicts by object identity in useOnyx #810
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
youssef-lr
merged 4 commits into
Expensify:main
from
callstack-internal:perf/memoize-useonyx-shallowequal
Jul 22, 2026
+122
−2
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
94767fa
Memoize shallowEqual verdicts by object identity in useOnyx
TMisiukiewicz 9073e97
fix prettier
TMisiukiewicz b2a8465
Extract memoizedShallowEqual to separate file and add unit tests
TMisiukiewicz 51bdc1b
Use 'should' pattern for test descriptions
TMisiukiewicz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import {shallowEqual} from 'fast-equals'; | ||
|
|
||
| /** | ||
| * Memoizes shallowEqual verdicts by the identity of the compared objects. Onyx values are | ||
| * treated as immutable (merge/set replace objects, never mutate), so a (prev, next) reference | ||
| * pair always yields the same verdict. In the hot case — N no-selector hooks on the same big | ||
| * key — every hook compares the exact same two cache-owned objects, so the first hook pays for | ||
| * the O(keys) walk and the rest resolve in O(1). WeakMap keys make stale entries impossible to | ||
| * read (lookup requires holding both exact objects) and let GC reclaim them. | ||
| */ | ||
| const shallowEqualVerdicts = new WeakMap<object, WeakMap<object, boolean>>(); | ||
|
|
||
| /** | ||
| * Identity-pair-memoized shallowEqual: same (a, b) references → cached verdict, no walk. | ||
| */ | ||
| function memoizedShallowEqual(a: unknown, b: unknown): boolean { | ||
| // Only object pairs are memoizable (WeakMap keys) — anything else is O(1) to compare anyway. | ||
| if (typeof a !== 'object' || a === null || typeof b !== 'object' || b === null) { | ||
| return shallowEqual(a, b); | ||
| } | ||
|
|
||
| let verdictsForA = shallowEqualVerdicts.get(a); | ||
|
|
||
| if (!verdictsForA) { | ||
| verdictsForA = new WeakMap(); | ||
| shallowEqualVerdicts.set(a, verdictsForA); | ||
| } | ||
|
|
||
| const cachedVerdict = verdictsForA.get(b); | ||
|
|
||
| if (cachedVerdict !== undefined) { | ||
| return cachedVerdict; | ||
| } | ||
|
|
||
| const verdict = shallowEqual(a, b); | ||
| verdictsForA.set(b, verdict); | ||
|
|
||
| return verdict; | ||
| } | ||
|
|
||
| export default memoizedShallowEqual; |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import memoizedShallowEqual from '../../lib/memoizedShallowEqual'; | ||
|
|
||
| describe('memoizedShallowEqual', () => { | ||
| describe('shallowEqual semantics', () => { | ||
| it('should return true for the same reference', () => { | ||
| const obj = {a: 1}; | ||
| expect(memoizedShallowEqual(obj, obj)).toBe(true); | ||
| }); | ||
|
|
||
| it('should return true for different references with shallowly-equal content', () => { | ||
| const member = {name: 'John'}; | ||
| expect(memoizedShallowEqual({a: 1, member}, {a: 1, member})).toBe(true); | ||
| }); | ||
|
|
||
| it('should return false when a top-level value differs', () => { | ||
| expect(memoizedShallowEqual({a: 1}, {a: 2})).toBe(false); | ||
| }); | ||
|
|
||
| it('should return false when key counts differ', () => { | ||
| expect(memoizedShallowEqual({a: 1}, {a: 1, b: 2})).toBe(false); | ||
| }); | ||
|
|
||
| it('should return false for equal deep content with different nested references', () => { | ||
| // Shallow, not deep: nested objects are compared by reference. | ||
| expect(memoizedShallowEqual({member: {name: 'John'}}, {member: {name: 'John'}})).toBe(false); | ||
| }); | ||
|
|
||
| it('should handle non-object inputs', () => { | ||
| expect(memoizedShallowEqual(undefined, undefined)).toBe(true); | ||
| expect(memoizedShallowEqual(undefined, {})).toBe(false); | ||
| expect(memoizedShallowEqual('a', 'a')).toBe(true); | ||
| expect(memoizedShallowEqual('a', 'b')).toBe(false); | ||
| expect(memoizedShallowEqual(1, 1)).toBe(true); | ||
| expect(memoizedShallowEqual(NaN, NaN)).toBe(true); | ||
| }); | ||
|
|
||
| it('should handle arrays', () => { | ||
| expect(memoizedShallowEqual([1, 2], [1, 2])).toBe(true); | ||
| expect(memoizedShallowEqual([1, 2], [1, 3])).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('memoization', () => { | ||
| it('should return the cached verdict for the same object pair without re-comparing', () => { | ||
| const a = {name: 'John'}; | ||
| const b = {name: 'Jane'}; | ||
| expect(memoizedShallowEqual(a, b)).toBe(false); | ||
|
|
||
| // Mutate `b` so the objects are now content-equal. Onyx values are immutable, | ||
| // so the memo is expected to keep returning the verdict computed for this exact | ||
| // (a, b) pair — proving the second call resolved from the cache, not a re-compare. | ||
| b.name = 'John'; | ||
| expect(memoizedShallowEqual(a, b)).toBe(false); | ||
| }); | ||
|
|
||
| it('should cache verdicts per pair, not per object', () => { | ||
| const a = {x: 1}; | ||
| const equalToA = {x: 1}; | ||
| const differentFromA = {x: 2}; | ||
|
|
||
| expect(memoizedShallowEqual(a, equalToA)).toBe(true); | ||
| expect(memoizedShallowEqual(a, differentFromA)).toBe(false); | ||
|
|
||
| // Both verdicts are retained independently for the same `a`. | ||
| expect(memoizedShallowEqual(a, equalToA)).toBe(true); | ||
| expect(memoizedShallowEqual(a, differentFromA)).toBe(false); | ||
| }); | ||
|
|
||
| it('should not memoize non-object inputs', () => { | ||
| // Primitives cannot be WeakMap keys; these calls must not throw and must compare directly. | ||
| expect(memoizedShallowEqual(1, {})).toBe(false); | ||
| expect(memoizedShallowEqual({}, 1)).toBe(false); | ||
| expect(memoizedShallowEqual(null, null)).toBe(true); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.