-
Notifications
You must be signed in to change notification settings - Fork 648
Hectahertz/selectpanel performance #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
|
|
👋 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.
| const set = new Set<string | number | ItemInput>() | ||
| for (const item of items) { | ||
| if (item.id !== undefined) { | ||
| set.add(item.id) | ||
| } else { | ||
| set.add(item) | ||
| } | ||
| } |
Copilot
AI
Feb 2, 2026
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.
The logic for computing a key from an item (if (item.id !== undefined) { set.add(item.id) } else { set.add(item) }) is duplicated here and again in the selectedItemsSet and selectedOnSortSet constructions, as well as mirrored in the has(...) checks. To keep the equality semantics centralized and reduce the risk of these code paths diverging in the future, consider extracting a small helper (e.g., a getItemKey function) and reusing it in all Set add/has sites.
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