Skip to content

Commit 83113d3

Browse files
perf: optimize starter icons resolution (#1300)
Replaces the O(N) full `Map` allocation over the entire icons array with a `Set` for wanted keys (O(K)) and a single-pass `for` loop that breaks early once all required starter icons are found. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent bb37c72 commit 83113d3

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

‎src/components/viewer/quick-import.tsx‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,32 @@ export function QuickImport({ onImport, className }: QuickImportProps) {
6060

6161
const starterIcons = useMemo(() => {
6262
if (!icons) return [];
63-
const lookup = new Map<string, IconEntry>();
63+
64+
// ⚡ Bolt: Prevent full Map allocation of O(N) by using a Set for wanted keys
65+
// and breaking early once all items are found.
66+
const wanted = new Set(STARTER_SLUGS);
67+
const result: IconEntry[] = [];
68+
6469
for (let i = 0; i < icons.length; i++) {
65-
lookup.set(icons[i].slug, icons[i]);
70+
if (wanted.has(icons[i].slug)) {
71+
result.push(icons[i]);
72+
if (result.length === wanted.size) break;
73+
}
6674
}
67-
return STARTER_SLUGS.map((s) => lookup.get(s)).filter(
68-
(x): x is IconEntry => Boolean(x),
75+
76+
// Restore the original intended ordering
77+
return result.sort(
78+
(a, b) => STARTER_SLUGS.indexOf(a.slug) - STARTER_SLUGS.indexOf(b.slug),
6979
);
7080
}, [icons]);
7181

7282
const searchableIcons = useMemo(() => {
7383
if (!icons) return [];
7484
return icons.map((icon) => ({
7585
icon,
76-
searchString: [icon.slug, icon.title, ...icon.aliases].join("\n").toLowerCase(),
86+
searchString: [icon.slug, icon.title, ...icon.aliases]
87+
.join("\n")
88+
.toLowerCase(),
7789
}));
7890
}, [icons]);
7991

0 commit comments

Comments
 (0)