Skip to content

perf(Collection): replace indexOf sort with node index map in getItems#2695

Merged
zernonia merged 2 commits into
v2from
perf/collection-getitems-index-map
Jun 11, 2026
Merged

perf(Collection): replace indexOf sort with node index map in getItems#2695
zernonia merged 2 commits into
v2from
perf/collection-getitems-index-map

Conversation

@zernonia

@zernonia zernonia commented Jun 11, 2026

Copy link
Copy Markdown
Member

Summary

getItems() sorted registered items into DOM order using Array.indexOf inside the sort comparator, re-scanning orderedNodes on every comparison — O(n²·log n). getItems() is called from ~23 files on every arrow-key nav, typeahead keystroke, and highlight change (Listbox, Combobox, Select, Menu, Tabs…), so large collections paid hundreds of thousands of scans per keypress.

This builds a node→index Map once and sorts by lookup — O(n·log n) with two O(n) passes, identical output. The ?? -1 fallback preserves prior behavior for items whose ref isn't in the DOM query result (detached nodes sort first, exactly as indexOf returning -1 did).

Test Plan

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Performance Improvements

    • Optimized item ordering computation in collections for improved performance.
  • Chores

    • Updated typo checking configuration.

@coderabbitai

coderabbitai Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 8ec074ac-cabd-40f7-9bb9-5493bf120c7d

📥 Commits

Reviewing files that changed from the base of the PR and between 9a87083 and cc02dba.

📒 Files selected for processing (2)
  • packages/core/src/Collection/Collection.ts
  • typos.toml

📝 Walkthrough

Walkthrough

This PR optimizes the item sorting logic in the Collection component by replacing repeated DOM node lookups with a precomputed index map, and updates the typo checker configuration to exempt the ba token from corrections.

Changes

Collection Sorting and Configuration Updates

Layer / File(s) Summary
Collection sorting optimization
packages/core/src/Collection/Collection.ts
The getItems method precomputes an orderMap lookup from DOM nodes to their order indices, replacing repeated indexOf calls in the sort comparator. Items not in the DOM retain their -1 index via ?? -1 fallback, preserving existing filter and ordering behavior.
Typo checker configuration
typos.toml
Adds ba to the [default.extend-words] configuration to prevent false-positive typo corrections on that token used in typeahead test fixtures.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 A map of order, swift and keen,
Replaces searches in between,
While ba now rests from typo's spell—
Small optimizations done so well! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and accurately describes the main performance optimization: replacing indexOf-based sorting with a node index map in the getItems method.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch perf/collection-getitems-index-map

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@pkg-pr-new

pkg-pr-new Bot commented Jun 11, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/reka-ui@2695

commit: cc02dba

getItems() sorted registered items into DOM order using Array.indexOf
inside the sort comparator, scanning orderedNodes on every comparison
(O(n² log n)). Build a node→index Map once and sort by lookup instead,
giving O(n log n) with two O(n) passes — identical output.

The `?? -1` fallback preserves prior behavior for items whose ref is not
in the DOM query result (indexOf returned -1, sorting them first).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@zernonia zernonia force-pushed the perf/collection-getitems-index-map branch from 9e6826a to e5e2fb9 Compare June 11, 2026 05:20
@zernonia zernonia changed the base branch from test/shared-composables-characterization to v2 June 11, 2026 05:21
The whole-repo typos scan flags the "BA"/"ba" typeahead search
fixtures in useTypeahead.test.ts as a misspelling of "by"/"be".
Allowlist "ba" in extend-words (case-insensitive, covers "BA" too),
mirroring the existing "Januar" entry.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@zernonia zernonia merged commit a0f3d52 into v2 Jun 11, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant