@@ -26,6 +26,19 @@ export interface ContributorsDataset {
2626
2727const 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+
2942function 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
4959export 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
5866export function listDocContributors ( ) : DocContributorsRecord [ ] {
0 commit comments