83207: debounce autocomplete query and memoize search options#86925
83207: debounce autocomplete query and memoize search options#86925abbasifaizan70 wants to merge 21 commits intoExpensify:mainfrom
Conversation
Codecov Report❌ Looks like you've decreased code coverage for some files. Please write tests to increase, or at least maintain, the existing level of code coverage. See our documentation here for how to interpret this table.
|
|
@aimane-chnaif Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button] |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4e8ba5a482
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
|
PR doesn’t need product input as a performance PR. Unassigning and unsubscribing myself. |
|
Please merge main |
|
@aimane-chnaif Update with the latest main. Thanks |
|
🤖 Code Review Overall this is a solid performance improvement — the debounce strategy and regex pre-compilation are both sound. A few items to consider: 1. Stale query fallback on line 350 (nit) autocompleteQueryValue={textInputValue ? debouncedAutocompleteQueryValue || textInputValue : ''}The 2. const valueToSearch = searchText?.replaceAll(new RegExp(/ /g), '');Wrapping a regex literal in 3. Perf tests look good — the No blocking issues — the changes are correct and well-scoped. 👍 |
| @@ -507,18 +507,15 @@ function getAlternateText( | |||
| * Searches for a match when provided with a value | |||
| */ | |||
| function isSearchStringMatch(searchValue: string, searchText?: string | null, participantNames = new Set<string>(), isReportChatRoom = false): boolean { | |||
There was a problem hiding this comment.
Can you please explain changes in this function?
What's this refactor for? Does this improve performance?
And add unit tests
There was a problem hiding this comment.
@aimane-chnaif This change is mainly a perf cleanup in a hot path, not intended to change behavior. Before, isSearchStringMatch() was creating a new RegExp inside the loop for every search word. Since this function runs across lots of options while typing, that adds unnecessary work on each keystroke.
What I changed:
- normalize/dedupe the words once
- compile the regexes once per function call
- reuse them in the loop
- return early on first mismatch
So matching logic stays the same, but we avoid repeated regex allocations and reduce JS work during search filtering.
There was a problem hiding this comment.
ok, please add unit test (not perf-test) for this function
There was a problem hiding this comment.
@aimane-chnaif Added unit test in OptionsListUtilsTest.tsx covering the refactored isSearchStringMatch path (multi-word normalization behavior), and it passes.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e1d2ccc2af
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
|
@aimane-chnaif I added Unit test cases. |
|
🤖 Code Review All CI checks pass. The debounce strategy and regex pre-compilation are correct. Two items worth addressing: 1. Unused debounce timer on const [textInputValue, , setTextInputValue] = useDebouncedState('', 500);The second element (debounced value) is never read — only the immediate value is used. This creates a 500ms debounce timer + state that serves no purpose. Consider using plain 2. First-keystroke empty flash (line 350) autocompleteQueryValue={textInputValue === '' ? '' : debouncedAutocompleteQueryValue}When the user types the first character, The No blocking issues. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e5e514dcdc
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
|
@aimane-chnaif PR is ready for review. Thanks. |
Explanation of Change
The
SearchRouterwas passingautocompleteQueryValue(updated viauseStateon every keystroke) directly toSearchAutocompleteList, triggering synchronousgetSearchOptions()+combineOrderingOfReportsAndPersonalDetails()+isSearchStringMatch()on every character typed, freezing the JS thread on mobile.Structural optimizations (debouncing + memoization prevent redundant work):
useDebouncedStateforautocompleteQueryValue: ReplacesuseStateso the expensiveSearchAutocompleteListfiltering only runs after the user pauses typing, not on every keystroke. The immediate value still drives arrow-key navigation and contextual logic.useMemoforsearchOptions: WrapsgetSearchOptions()so it only re-runs when its 13 dependencies actually change, instead of re-computing on every render.useMemoforrecentReportsOptions: WrapscombineOrderingOfReportsAndPersonalDetails()so sorting/filtering is skipped whenautocompleteQueryValueandsearchOptionshaven't changed.Algorithmic improvement:
isSearchStringMatch:new RegExp()per word per item → compile once per call, reuse across all itemsisSearchStringMatch:matchingflag withcontinuepast failures → earlyreturn falseon first mismatchsearchWords:new Set()keeping empty strings →Array.from(new Set(...)).filter(Boolean)Performance (Reassure, 40K reports):
main(baseline)getFilteredOptionswith searchfilterAndOrderOptions(40K items, 3-word query)Fixed Issues
$ #83207
PROPOSAL: #83207 (comment)
Tests
Offline tests
Same as tests.
QA Steps
Same as tests.
PR Author Checklist
### Fixed Issuessection aboveTestssectionOffline stepssectionQA stepssectioncanBeMissingparam foruseOnyxtoggleReportand notonIconClick)src/languages/*files and using the translation methodSTYLE.md) were followedAvatar, I verified the components usingAvatarare working as expected)StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))npm run compress-svg)Avataris modified, I verified thatAvataris working as expected in all cases)Designlabel and/or tagged@Expensify/designso the design team can review the changes.ScrollViewcomponent to make it scrollable when more elements are added to the page.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
Screen.Recording.2026-04-02.at.4.04.17.AM.mov
Android: mWeb Chrome
Screen.Recording.2026-04-02.at.4.05.33.AM.mov
iOS: Native
Screen.Recording.2026-04-02.at.3.57.50.AM.mov
iOS: mWeb Safari
Screen.Recording.2026-04-02.at.3.58.48.AM.mov
MacOS: Chrome / Safari
Screen.Recording.2026-04-02.at.3.56.08.AM.mov