-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve performance of Combobox
component
#2574
Merged
Merged
Conversation
This file contains 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
No need to spread here because this will slow things down tremendously. If you add 5 options, then this will happen: ```js let options = [] options = [...options, 1] options = [...options, 2] options = [...options, 3] options = [...options, 4] options = [...options, 5] ``` It's making a lot of copies and has to spread everything it just copied again. Instead, let's just push to the array: ```js let options = [] options.push(1) options.push(2) options.push(3) options.push(4) options.push(5) ```
When registering 50 options, we will re-sort the options based on the DOM node position for every option that gets registered. This is overkill and unnecessary. The re-sorting is useful if an option is injected between 2 options. When we sort the full list for _every_ `registerOption` call then the result after the last `registerOption` will be the same if we only sorted after the last `registerOption` call only. This will ensure that we are skipping a ton of work and only do the work once at the end where it matters.
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
RobinMalfait
changed the title
WIP: Improve performance of
Improve performance of Jul 4, 2023
Combobox
componentCombobox
component
RobinMalfait
force-pushed
the
fix/issue-2527
branch
from
July 5, 2023 08:52
b557346
to
8e35274
Compare
This will allow us to perform the task of `goToOption` once instead of `n` times if they happen really fast after eachother. This can happen when you are leaving option B and going to option A. Then the following steps happen: - Leaving Option B `goToOption(Nothing)` - Entering Option A `goToOption(A)` Both will re-render everything because the internal active option index would be changed. But only the last `goToOption(A)` is the interesting version here. We could also move the `goToOption(Nothing)` to the `ComboboxOptions` (wrapper) itself instead of adding these listeners on each individual `ComboboxOption`. However, if you add an additional visual piece of DOM above or between the options, hovering that would not leave the `ComboboxOptions` and therefore the last `ComboboxOption` would still be active which we don't want.
RobinMalfait
force-pushed
the
fix/issue-2527
branch
from
July 5, 2023 09:00
8e35274
to
e473da6
Compare
This was referenced Jul 5, 2023
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 improves the performance of the
Combobox
component in Vue by reducing the amount of work that has to be done.A few things that are optimized in this PR:
Optimize registering options (A) — When registering an option, we have to make sure that all our options are sorted based on the DOM order. This is necessary if you use the arrow keys to go to the previous/next option, and if you just added an option between 2 other options. This is now optimized to only do the sorting once every option is registered instead of triggering a re-render for every single option you render.
Optimize registering options (B) — When a new option was registered, we created a new array by spreading the old array in (
[...options, newOption]
), now we just push to the array.Optimize going to an option — When you hover from option A to option B, then 2 events happen:
handleLeave(option A)
which runsgoToOption(Nothing)
handleEnter(option B)
which runsgoToOption(option B)
These are 2 actions that will re-render the
Combobox
component, but only the last one is interesting when moving between options. ThehandleLeave(option A)
is still necessary if you move away from an option to outside of theCombobox
itself, but not in this scenario.To summarize:
Fixes: #2571
Fixes: #2527