Description
Problem
When reports are selected in Spend, useSearchBulkActions checks every selected report to decide whether to offer "Change workspace". Both checks behind it, isChangeWorkspaceAction and canChangeWorkspaceOfSearchReport, loop over Object.values(policies). That builds a new array of every workspace for each selected report. On an account with about 6,000 workspaces, selecting a page of reports copies the whole collection once per report.
Solution
A new getPolicyList helper in PolicyUtils lists the workspaces once per frozen policy collection snapshot and reuses that list. A changed workspace always gets a fresh list. Both checks loop over the cached list, and their results are unchanged. canChangeWorkspaceOfSearchReport dropped from 169 ms to 8 ms when selecting 50 reports.
PR
Issue Owner
Current Issue Owner: @DylanDylannUpwork Automation - Do Not Edit
- Upwork Job URL: https://www.upwork.com/jobs/~022127794249746406353
- Upwork Job ID: 2127794249746406353
- Last Price Increase: 2026-12-01
Activity
expensify-chore-bot commentedon Dec 1, 2026
Current assignee @DylanDylann is eligible for the External assigner, not assigning anyone new.
[-]perf: Reuse one policy list across selected reports in the change-workspace check[/-][+][$175] perf: Reuse one policy list across selected reports in the change-workspace check[/+]expensify-chore-bot commentedon Dec 1, 2026
Job added to Upwork: https://www.upwork.com/jobs/~022127794249746406353
MelvinBot commentedon Dec 1, 2026
Proposal
Confidence: Medium — the code supports the allocation bottleneck and proposed fix; browser performance measurements were not independently reproduced.
What is the root cause of that problem?
The change-workspace eligibility checks repeatedly build arrays containing every workspace, even when their subsequent loops stop early. With thousands of workspaces and many selected reports, this creates unnecessary enumeration and allocation.
The bulk-actions hook stops at the first movable report, so it does not always check every selected report. The worst case is when none can move. The workspace-selection page checks every resolved selected report.
Code evidence
ReportSecondaryActionUtils.ts:715–746eagerly callsObject.valuesbefore looping. Breaking early cannot avoid that allocation.SearchUIUtils.ts:8445–8468creates another array if the first eligibility check succeeds.SearchChangeWorkspacePage.tsx:61–115applies these checks across selected reports.These arrays contain policy references; they are not deep copies of workspace data.
What changes do you think we should make in order to solve the problem?
The existing Reuse one policy list across selected reports PR addresses this bottleneck appropriately: share one policy list per frozen collection identity, while continuing to evaluate eligibility separately for each report. Review that PR rather than opening another implementation.
Before merging, extend regression coverage for workspace additions/removals and repeat the large-account browser profile.
Correctness checks and validation limits
getPolicyListhelper uses aWeakMap, caches only frozen collections, and rebuilds lists for mutable inputs.useOnyx.ts:63–90.What alternative solutions did you explore? (Optional)
Compute the policy list once in each caller and pass it through both helpers. That avoids shared caching but requires broader signature changes.
Related: Share policy collection scans between subscribers addresses similar repeated scanning across subscribers, rather than across selected reports.
Next Steps for Contributor+ team:
To accept:
@MelvinBot implement [this](https://github.com/Expensify/App/issues/108489)to create a draft PR—note that an implementation already exists above.To refine:
@MelvinBot <your feedback>To reject: Explain why you are rejecting Melvin's proposal.
view run