门禁通过质量分71· 覆盖率80% · 严重问题 0,可安全合并。
工作台资源列表聚合与排序优化
hancloud-webfeat/dashboard-list沈知微 · 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+ // 每个 item 都全量扫一遍 tags
A
AI 审查官AI重要
O(n²) 复杂度:items × tags 双重循环。资源列表上千条时会明显卡顿。应先把 tags 建成 Map<id, name> 再 O(1) 查找。
建议修改
+5−9
1- const result: Enriched[] = [];
2- for (const it of items) {
3- // 每个 item 都全量扫一遍 tags
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 审查官AI次要
双层循环里 item 无匹配 tag 时会被直接丢弃,与旧实现(保留 item、tagName 为 undefined)行为不一致,可能造成列表条目缺失。
10+ }
11+ // 同步刷新缓存
12+ refreshCache(result);
A
AI 审查官AI提示
refreshCache 为同步副作用混在纯数据构建函数里,建议拆分以保持 buildList 可测试、无副作用。
13+ return result.sort((a, b) => a.tagName.length - b.tagName.length);
614 }
A
AI 审查官AI重要
空指针风险:tagName 可能 undefined(item 无匹配 tag),sort 里 a.tagName.length 会抛 TypeError。需提供默认值或过滤。