refactor: speed up option selection state in useSelect#4611
Open
TrevorBurnham wants to merge 3 commits into
Open
refactor: speed up option selection state in useSelect#4611TrevorBurnham wants to merge 3 commits into
TrevorBurnham wants to merge 3 commits into
Conversation
useSelect recomputed selection state on every Select/Multiselect render with two nested scans that both grew quadratically with large lists: - connectOptionsByValue scanned all dropdown options for each selected option, an O(selected × options) lookup. - getOptionProps then ran up to three __selectedOptions.indexOf() scans per rendered option (the option plus its two neighbours), O(filtered × selected). On the default non-virtualized PlainList this scans the whole list. Index the dropdown options by value in a Map (O(selected + options)) and test selection membership through a Set built once per render (O(1) per option). The Set is keyed by the same DropdownOption references connectOptionsByValue returns, so Set.has() is exactly equivalent to the previous reference-based indexOf() > -1 checks, including the undefined-neighbour case at list boundaries. Benchmarks (Node): connectOptionsByValue, 2000 options, 200 selected: 993µs -> 27µs (~37x) getOptionProps, 1000 options, 100 selected: 50µs -> 12µs (~4x) Behaviour is unchanged: the first non-parent option for a given value still wins, and empty selections, undefined values, and duplicate values are preserved. Existing use-select selection assertions (selected / isNextSelected / isPreviousSelected, incl. the group-neighbour case) plus new connect-options unit tests cover the behaviour.
gethinwebster
left a comment
Member
There was a problem hiding this comment.
Looks good but please remove the inline comments, they're adding unnecessary explanations.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4611 +/- ##
=======================================
Coverage 97.50% 97.50%
=======================================
Files 948 948
Lines 30314 30317 +3
Branches 11046 11047 +1
=======================================
+ Hits 29559 29562 +3
+ Misses 748 708 -40
- Partials 7 47 +40 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Co-authored-by: Gethin Webster <gethinw@amazon.de>
Co-authored-by: Gethin Webster <gethinw@amazon.de>
gethinwebster
approved these changes
Jun 12, 2026
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.
This PR speeds up the selection-state computation that runs on every render in
useSelect, which backs bothSelectandMultiselect.Previously,
useSelectrecomputed which options are selected using two nested scans that grew quadratically with the size of the options and selection lists:connectOptionsByValuescanned all dropdown options to find a match for each selected option —O(selected × options).getOptionPropsthen ran up to three__selectedOptions.indexOf()scans per rendered option (the option itself plus its two neighbours, used to computeselected/isNextSelected/isPreviousSelected) —O(filtered × selected). On the default, non-virtualizedPlainListthis scans the entire list, so the cost is not bounded by the visible window.For a large multiselect, this adds up to hundreds of microseconds of avoidable work on every render.
This change makes both lookups linear:
connectOptionsByValuebuilds aMapof dropdown options keyed by value once, then resolves each selected option inO(1)→O(selected + options).useSelectbuilds aSetof the selected dropdown options once per render and tests membership inO(1)per option, replacing the per-optionindexOfscans.The
Setis keyed by the sameDropdownOptionreferences thatconnectOptionsByValuereturns, soSet.has()is exactly equivalent to the previous reference-basedindexOf() > -1checks — including theundefined-neighbour case at list boundaries.Benchmarks (Node, isolated microbenchmarks with an equivalence check):
connectOptionsByValueconnectOptionsByValuegetOptionProps(over all options)How has this been tested?
src/select/__tests__/connect-options.test.tscover the edge cases that must be preserved: empty selection, order preservation, parent options skipped, first-match-wins on duplicate values, and matching options with anundefinedvalue.getOptionPropsis covered by the existingsrc/select/__tests__/use-select.test.tsassertions onselected/isNextSelected/isPreviousSelected, including the group-neighbour case.Select,Multiselect, andAutoselectunit suites pass (src/select/,src/multiselect/,src/autosuggest/— 552 tests green).By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.