Skip to content

Commit a0f3d52

Browse files
zernoniaclaude
andauthored
perf(Collection): replace indexOf sort with node index map in getItems (#2695)
* perf(Collection): replace indexOf sort with node index map in getItems 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> * ci: allow "ba" typeahead fixture in typos config 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> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 9a87083 commit a0f3d52

2 files changed

Lines changed: 4 additions & 1 deletion

File tree

packages/core/src/Collection/Collection.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ export function useCollection<ItemData = {}>(options: { key?: string, isProvider
3333
if (!collectionNode)
3434
return []
3535
const orderedNodes = Array.from(collectionNode.querySelectorAll(`[${ITEM_DATA_ATTR}]`))
36+
const orderMap = new Map(orderedNodes.map((node, index) => [node, index]))
3637
const items = Array.from(context.itemMap.value.values())
3738
const orderedItems = items.sort(
38-
(a, b) => orderedNodes.indexOf(a.ref) - orderedNodes.indexOf(b.ref),
39+
(a, b) => (orderMap.get(a.ref) ?? -1) - (orderMap.get(b.ref) ?? -1),
3940
)
4041

4142
if (includeDisabledItem)

typos.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ extend-ignore-re = [
77
[default.extend-words]
88
# Don't correct the german locale "January"
99
Januar = "Januar"
10+
# "ba" is a typeahead search fixture (typing to match "banana") in useTypeahead.test.ts
11+
ba = "ba"

0 commit comments

Comments
 (0)