-
Notifications
You must be signed in to change notification settings - Fork 652
perf(SelectPanel): Optimize performance by improving the lookup algorithms #7497
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
base: main
Are you sure you want to change the base?
Conversation
🦋 Changeset detectedLatest commit: e72b88d 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 |
|
👋 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
liuliu-dev
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✨
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