Quality gate passedQuality score71· Coverage rate80% · 0 critical findings; safe to merge.
Optimization of workbench resource list aggregation and sorting
hancloud-webfeat/dashboard-listShen Zhiwei · 2026-06-05 11:08src/dashboard/resourceService.ts+12−4
11 export function buildList(items: Item[], tags: Tag[]) {
2- return items.map((it) => ({
3- ...it,
4- tagName: tags.find((t) => t.id === it.tagId)?.name,
5- }));
2+ const result: Enriched[] = [];
3+ for (const it of items) {
4+ // Scans every tag for every item
A
AI reviewerAIMajor
O(n²) complexity: the nested items × tags loop will slow down on large lists. Build a Map<id, name> for tags and perform O(1) lookups instead.
Suggested change
+5−9
1-const result: Enriched[] = [];
2-for (const it of items) {
3- // Scans every tag for every item
4- for (const t of tags) {
5- if (t.id === it.tagId) {
6- result.push({ ...it, tagName: t.name });
7- }
8- }
9-}
1+ const tagMap = new Map(tags.map((t) => [t.id, t.name]));
2+ const result: Enriched[] = items.map((it) => ({
3+ ...it,
4+ tagName: tagMap.get(it.tagId),
5+ }));
5+ for (const t of tags) {
6+ if (t.id === it.tagId) {
7+ result.push({ ...it, tagName: t.name });
8+ }
9+ }
A
AI reviewerAIMinor
The nested loop drops items without matching tags. The previous implementation kept those items with an undefined tagName, so this refactor can remove rows unexpectedly.
10+ }
11+ // Refresh the cache synchronously
12+ refreshCache(result);
A
AI reviewerAIInfo
refreshCache introduces a side effect into a data-building function. Move it out so buildList remains pure and easy to test.
13+ return result.sort((a, b) => a.tagName.length - b.tagName.length);
614 }
A
AI reviewerAIMajor
tagName can be undefined when an item has no matching tag, causing a.tagName.length to throw during sorting. Provide a safe default or filter unmatched items explicitly.