[Autocomplete] Optimize selected option lookup in useAut…#47953
Open
anchmelev wants to merge 1 commit intomui:masterfrom
Open
[Autocomplete] Optimize selected option lookup in useAut…#47953anchmelev wants to merge 1 commit intomui:masterfrom
anchmelev wants to merge 1 commit intomui:masterfrom
Conversation
…ocomplete Use a Set-based fast path when isOptionEqualToValue is the default strict equality comparator. This avoids quadratic selected-state checks in large multiple selections while preserving custom equality semantics. Also add a regression test to ensure custom isOptionEqualToValue behavior is unchanged.
Netlify deploy previewhttps://deploy-preview-47953--material-ui.netlify.app/ Bundle size report
|
Author
|
Could a maintainer please add a label (e.g. |
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.
Closes #47843
Summary
This PR improves
Autocompleteperformance inmultiplemode with large datasets by optimizing selected-option lookups inuseAutocomplete.Problem
When many options are selected (for example, 10,000), selected-state checks are done repeatedly with linear scans (
Array.some) in hot paths:filterSelectedOptionsaria-selectedviagetOptionProps)That leads to quadratic work and visible lag.
What changed
Setof selected values once per render whenisOptionEqualToValueis the default (option === value)Set.has(option)for O(1) lookupisOptionEqualToValueis customized, keep usingsome(...)to preserve user-defined semanticsisOptionSelectedhelper in both hot paths:filterSelectedOptionsgetOptionProps(aria-selected)isOptionEqualToValuebehavior is unchanged.Why this approach
Set.hasis only correct for strict reference equality.For custom comparators (for example, compare by
id),Set.hascan be incorrect, so this PR explicitly falls back to comparator-based scans in that case.This gives a large perf win for the default path without changing behavior for customized equality.
Benchmark notes
Local micro-benchmark (10,000 options, 10,000 selected, two selected-state passes):
Tests
pnpm prettierpnpm eslint packages/mui-material/src/useAutocomplete/useAutocomplete.js packages/mui-material/src/useAutocomplete/useAutocomplete.test.jspnpm test:node useAutocomplete