Skip to content

Conversation

@hectahertz
Copy link
Contributor

@hectahertz hectahertz commented Feb 2, 2026

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:

  • Checking if an item is currently selected (isItemCurrentlySelected)
  • Sorting selected items to the top (shouldOrderSelectedFirst)
  • Filtering selected items not in the current view (handleSelectAllChange)
  • For panels with many items or selections, this caused noticeable performance degradation.

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

  • Patch release
  • Minor release
  • Major release; if selected, include a written rollout or migration plan
  • None; if selected, include a brief description as to why

Testing & Reviewing

Merge checklist

@hectahertz hectahertz requested a review from a team as a code owner February 2, 2026 18:06
@changeset-bot
Copy link

changeset-bot bot commented Feb 2, 2026

🦋 Changeset detected

Latest commit: e72b88d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@primer/react Patch

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

@github-actions github-actions bot added the integration-tests: recommended This change needs to be tested for breaking changes. See https://arc.net/l/quote/tdmpakpm label Feb 2, 2026
@github-actions
Copy link
Contributor

github-actions bot commented Feb 2, 2026

👋 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 integration-tests: skipped manually label to skip these checks.

Copy link
Contributor

Copilot AI left a 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 itemsInViewSet to speed up “Select all” handling, especially when filtering, by doing O(1) membership checks instead of items.some(...).
  • Added selectedItemsSet to make isItemCurrentlySelected O(1) for multi-select panels.
  • Updated itemsToRender sorting to precompute a selectedOnSortSet and use it for determining whether items should be ordered first when shouldOrderSelectedFirst is enabled.

Copy link
Contributor

@liuliu-dev liuliu-dev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hectahertz hectahertz changed the title Hectahertz/selectpanel performance Improve SelectPanel performance Feb 5, 2026
@github-actions github-actions bot requested a deployment to storybook-preview-7497 February 5, 2026 15:39 Abandoned
@github-actions github-actions bot temporarily deployed to storybook-preview-7497 February 5, 2026 15:48 Inactive
@hectahertz hectahertz enabled auto-merge February 6, 2026 12:27
@hectahertz hectahertz changed the title Improve SelectPanel performance perf(SelectPanel): Optimize performance by improving the lookup algorithms Feb 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

integration-tests: recommended This change needs to be tested for breaking changes. See https://arc.net/l/quote/tdmpakpm

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants