Skip to content

Commit 4d0ccd5

Browse files
committed
fix: contributor 构建哈希表,提高线性扫描效率
1 parent 342bba9 commit 4d0ccd5

1 file changed

Lines changed: 15 additions & 7 deletions

File tree

lib/contributors.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@ export interface ContributorsDataset {
2626

2727
const contributorsDataset = dataset as unknown as ContributorsDataset;
2828

29+
// 预先构建 Map 缓存,将查找的时间复杂度从 O(N) 降低到 O(1)
30+
const contributorsByPath = new Map<string, DocContributorsRecord>();
31+
const contributorsByDocId = new Map<string, DocContributorsRecord>();
32+
33+
for (const result of contributorsDataset.results) {
34+
if (result.path) {
35+
contributorsByPath.set(result.path, result);
36+
}
37+
if (result.docId) {
38+
contributorsByDocId.set(result.docId, result);
39+
}
40+
}
41+
2942
function normalizeRelativePath(relativePath: string): string {
3043
const cleaned = relativePath.replace(/^\/+/, "").replace(/\\/g, "/");
3144
return `app/docs/${cleaned}`;
@@ -40,19 +53,14 @@ export function getDocContributorsByPath(
4053
): DocContributorsRecord | null {
4154
if (!relativeDocPath) return null;
4255
const normalized = normalizeRelativePath(relativeDocPath);
43-
return (
44-
contributorsDataset.results.find((entry) => entry.path === normalized) ??
45-
null
46-
);
56+
return contributorsByPath.get(normalized) ?? null;
4757
}
4858

4959
export function getDocContributorsByDocId(
5060
docId: string | undefined | null,
5161
): DocContributorsRecord | null {
5262
if (!docId) return null;
53-
return (
54-
contributorsDataset.results.find((entry) => entry.docId === docId) ?? null
55-
);
63+
return contributorsByDocId.get(docId) ?? null;
5664
}
5765

5866
export function listDocContributors(): DocContributorsRecord[] {

0 commit comments

Comments
 (0)