[Due for payment 2026-11-26] [$175] [Performance] Onyx: skip unchanged cached objects when removing nested nulls on set
Problem
Every Onyx.set runs removeNestedNullValues over the whole value before caching and storing it. The App rewrites some large values repeatedly, especially derived values with one entry per report. On heavy accounts, each Pusher update takes ~350 ms on iOS and Android, and part of that cost scales with the stored value's size rather than the update's.
Solution (react-native-onyx)
- Keep the cache free of nulls. This restores the rule from Expensify/react-native-onyx#554 by fixing 4 paths that put nulls in the cache today: batched remove-then-replace merges, lazy SQLite reads, web cross-tab sync, and
Onyx.clear(). - Pass the cached value to
removeNestedNullValues. It then skips any nested object that is the same object as in the cache. This relies on the shared references kept since Expensify/react-native-onyx#765, and subscribers receive the same values. - Add a dev-only alert if App code mutates a cached object in place.
Behavior change
On those 4 paths, subscribers get a missing key instead of null. Testing on web and native found this only for account.errors, report.errorFields.export, and report.errorFields.notFound. All three treat null and missing the same way.
Results (heavy account, median before → after)
- Each Pusher update: iOS 347→331 ms (-4.6%), Android 349→336 ms (-3.7%), Web CPU x4 326→310 ms (-4.9%)
- Incremental reconnect: iOS ~700→~660 ms, Web CPU x4 602→564 ms (-6.6%)
- Setting a large object: iOS ~290→~265 ms, Android 293→282 ms
- Tested on top of the pending OnyxDerived no-op-write skip, the gain holds: Pusher update 341→316 ms (-7.2%), reconnect 580→532 ms (-5.5%), 5/5 runs faster.
- Most of the gain comes from
visibleReportActions,sortedReportActions, andoutstandingReportsByPolicyID.reportAttributesdoesn't benefit, because itsundefinedfields break sharing after a full recompute. Fixing that on the App side is a possible follow-up.
Verify in production
In Sentry, StartupData.Apply should get shorter for ReconnectApp on heavy accounts (~40 ms on iOS).
Links
- Onyx PR: https://github.com/Expensify/react-native-onyx/pull/856
- Background: https://github.com/Expensify/App/issues/71205
- Related: https://github.com/Expensify/App/pull/98956
Reported in Slack.
Issue Owner
Current Issue Owner: @mallenexpensifyUpwork Automation - Do Not Edit
- Upwork Job URL: https://www.upwork.com/jobs/~022109596033799181646
- Upwork Job ID: 2109596033799181646
- Last Price Increase: 2026-10-12
Let us know when the PR is ready
Triggered auto assignment to Contributor-plus team member for initial proposal review - @Krishna2323 (External)
Job added to Upwork: https://www.upwork.com/jobs/~022109596033799181646
Proposal
What is the root cause of that problem?
The cost is in react-native-onyx, not in App code. Onyx.set calls removeNestedNullValues on the entire new value and walks every nested object, even though most of those objects are the same references already held in the cache (shared since react-native-onyx#765). The walk cannot skip them because the cache cannot be trusted to be null-free: four paths still put nulls into it (batched remove-then-replace merges, lazy SQLite reads, web cross-tab sync, Onyx.clear()), which breaks the rule from react-native-onyx#554. App only consumes the library through the pinned dependency at https://github.com/Expensify/App/blob/37e2b8b8fb247f003796221219ebb979d78cff94/package.json#L204. I did not have the library source in this checkout, so the library-side description below follows the issue and the linked Onyx PR #856.
What changes do you think we should make in order to solve the problem?
- In react-native-onyx, strip nulls on the four paths above (batched remove-then-replace merge, lazy SQLite read, web cross-tab sync,
Onyx.clear()) so the cache never holds nulls. - Give
removeNestedNullValuesan optional cached-value argument and return early for any nested object that is===to the cached one, since a null-free cached object needs no scan. - Have
Onyx.setpass the current cached value into that call. - Add a dev-only check that warns when App code mutates a cached object in place, since the skip depends on cached objects staying unchanged.
- Add Onyx unit tests: a nested object that is the same reference as the cached one is not traversed, and each of the four paths leaves no nulls in the cache.
- Release react-native-onyx and bump the version in App's package.json (currently 3.0.116).
- In App, check that the only subscriber-visible change is
nullbecoming missing foraccount.errors,report.errorFields.exportandreport.errorFields.notFound. The issue says all three treat the two the same. - Optional follow-up in App: stop
reportAttributesfrom emittingundefinedfields so its shared references survive a full recompute.
What alternative solutions did you explore? (Optional)
- Skip only the OnyxDerived no-op writes in App: this is a separate fix that already exists, and it does not help when a value changes slightly while most of its nested objects stay the same.
- Keep scanning but cache a "null-free" flag per object: this needs an extra WeakSet lookup per object and is more complex than comparing against the cached reference.
Not looking for proposals, only wanted to get C+ assigned
https://github.com/Expensify/react-native-onyx/pull/856 - Its ready for review now
I'm OOO from tomorrow until the end of next week. Since I have a lot of context on this, I'd prefer to continue it myself when I'm back, including the review on react-native-onyx#856. @mountiny let me know if that's fine with you. If not, someone from Software Mansion will take it over in the meantime, thanks!
@arekm213 Huh... This is 4 days overdue. Who can take care of this?
@arekm213 Still overdue 6 days?! Let's take care of this!
I will continue working on this issue on Monday
@arekm213 Whoops! This issue is 2 days overdue. Let's get this updated quick!
Im currently focusing on this issue which seems higher priority for me at the moment, i will try to get back to this issue soon, hopefully this week
@arekm213 Uh oh! This issue is overdue by 2 days. Don't forget to update your issues!
Onyx PR is merged and released in 3.0.144, the App bump PR is on hold until the App catches up with NitroSQLite 10 and the Onyx.disconnect removal
@arekm213 Uh oh! This issue is overdue by 2 days. Don't forget to update your issues!
Proposal
Please re-state the problem that we are trying to solve in this issue.
Every Onyx.set call recursively traverses the entire value via removeNestedNullValues before updating the in-memory cache and persistent storage. For large derived or collection-like Onyx values that are frequently updated via Onyx.set (sharing most nested object references with the previously cached value), each update takes ~350 ms on iOS and Android because removeNestedNullValues re-walks unchanged nested objects on every call.
What is the root cause of that problem?
In react-native-onyx (lib/Onyx.ts and lib/OnyxUtils.ts):
removeNestedNullValues(value)currently accepts only the incomingvalueand recursively clones/walks every nested object and array, even when a nested property is the exact same object reference (===) as the corresponding property already stored incache.get(key).removeNestedNullValuescould not safely skip reference-equal cached subtrees (value[k] === cachedValue[k]) because 4 code paths inreact-native-onyxwere still able to leavenullvalues inside the in-memoryOnyxCache:- Batched remove-then-replace
Onyx.mergeoperations - Lazy SQLite storage reads hydrating into cache
- Web
localStoragecross-tab synchronization (onStorageEvent) Onyx.clear()with default key states
- Batched remove-then-replace
What changes do you think we should make in order to solve the problem?
In Expensify/react-native-onyx (and bumping the react-native-onyx dependency in Expensify/App/package.json):
- Ensure the in-memory
OnyxCacheis strictly null-free across all 4 remaining paths (batchedmergechanges, lazy SQLite reads, web cross-tab storage sync, andOnyx.clear()) by stripping nestednullvalues before inserting into cache. - Update
removeNestedNullValues(value, cachedValue)inOnyxUtils.tsto accept the currentcachedValuefromcache.get(key). When walking nested properties, ifnestedValue === cachedNestedValue, skip recursing into that subtree and reusecachedNestedValuedirectly (and in__DEV__, assert that the skipped cached subtree contains nonullvalues to catch in-place mutations of cached Onyx objects). - In
Expensify/App, bumpreact-native-onyxinpackage.jsononce thereact-native-onyxPR (#856) is released, and verify that callers readingaccount.errors,report.errorFields.export, andreport.errorFields.notFoundhandle omitted keys identically tonull.
What alternative solutions did you explore? (Optional)
We explored refactoring individual Onyx.set callers in Expensify/App (such as derived Onyx configs) to use Onyx.merge, but derived keys hold computed snapshots of entire collections and naturally share unchanged sub-object references; skipping reference-equal cached subtrees in removeNestedNullValues solves the O(N) traversal bottleneck for all Onyx.set callers at once.
📣 @jeff-graham-ai! 📣 Hey, it seems we don’t have your contributor details yet! You'll only have to do this once, and this is how we'll hire you on Upwork. Please follow these steps:
- Make sure you've read and understood the contributing guidelines.
- Get the email address used to login to your Expensify account. If you don't already have an Expensify account, create one here. If you have multiple accounts (e.g. one for testing), please use your main account email.
- Get the link to your Upwork profile. It's necessary because we only pay via Upwork. You can access it by logging in, and then clicking on your name. It'll look like this. If you don't already have an account, sign up for one here.
- Copy the format below and paste it in a comment on this issue. Replace the placeholder text with your actual details.
Format:
Contributor details
Your Expensify account email: <REPLACE EMAIL HERE>
Upwork Profile Link: <REPLACE LINK HERE>
@arekm213 Still overdue 6 days?! Let's take care of this!
@arekm213 8 days overdue is a lot. Should this be a Weekly issue? If so, feel free to change it!
We are still waiting for onyx update in mobile app