perf(SelectPanel): Optimize performance by improving the lookup algorithms#7497
Open
hectahertz wants to merge 11 commits intomainfrom
Open
perf(SelectPanel): Optimize performance by improving the lookup algorithms#7497hectahertz wants to merge 11 commits intomainfrom
hectahertz wants to merge 11 commits intomainfrom
Conversation
🦋 Changeset detectedLatest commit: 4ed9d7b The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Contributor
|
👋 Hi, this pull request contains changes to the source code that github/github-ui depends on. If you are GitHub staff, test these changes with github/github-ui using the integration workflow. Or, apply the |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR focuses on improving the performance of SelectPanel by replacing repeated linear scans over the items/selected arrays with precomputed Set-based lookups.
Changes:
- Added
itemsInViewSetto speed up “Select all” handling, especially when filtering, by doing O(1) membership checks instead ofitems.some(...). - Added
selectedItemsSetto makeisItemCurrentlySelectedO(1) for multi-select panels. - Updated
itemsToRendersorting to precompute aselectedOnSortSetand use it for determining whether items should be ordered first whenshouldOrderSelectedFirstis enabled.
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 https://github.com/github/primer/issues/5788
This PR improves the performance of SelectPanel by optimizing how selected items are looked up during rendering and sorting.
The previous implementation used O(n) or O(n*m) array lookups when:
Changelog
First, the sorting algorithm in itemsToRender was slow. For each comparison during sorting, it was calling selectedOnSort.some with Object.entries and every, checking all properties of all selected items. I replaced this with a pre-computed Set of selected item IDs that allows instant lookups.
Second, the isItemCurrentlySelected function was iterating through all selected items every time it was called. Since it gets called for every item during the map operation, this was doing a lot of redundant work. I added a memoized selectedItemsSet that gets computed once when the selected prop changes, making each lookup instant.
Third, the handleSelectAllChange function had the same problem. It was using items.some inside a filter, iterating through all visible items for each selected item. I added a memoized itemsInViewSet to make those lookups instant as well.
Rollout strategy
Testing & Reviewing
Merge checklist