mirror of
https://github.com/jsdoc/jsdoc.git
synced 2025-12-08 19:46:11 +00:00
fix: speed up templateHelper's getAncestor() by 500x
Backported from #2150, authored by @mrdoob.
This commit is contained in:
parent
11a99cfb5d
commit
cbea149ae8
@ -805,6 +805,26 @@ exports.getSignatureReturns = ({yields, returns}, cssClass) => {
|
||||
return returnTypes;
|
||||
};
|
||||
|
||||
// Cache for memberof index to speed up ancestor lookups
|
||||
let memberofIndex = null;
|
||||
|
||||
function buildMemberofIndex(data) {
|
||||
if (memberofIndex) {
|
||||
return memberofIndex;
|
||||
}
|
||||
|
||||
memberofIndex = new Map();
|
||||
const allDoclets = data().get();
|
||||
|
||||
for (const doclet of allDoclets) {
|
||||
if (doclet.longname) {
|
||||
memberofIndex.set(doclet.longname, doclet);
|
||||
}
|
||||
}
|
||||
|
||||
return memberofIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve an ordered list of doclets for a symbol's ancestors.
|
||||
*
|
||||
@ -818,9 +838,13 @@ exports.getAncestors = (data, doclet) => {
|
||||
let doc = doclet;
|
||||
let previousDoc;
|
||||
|
||||
// Build index once for all lookups
|
||||
const index = buildMemberofIndex(data);
|
||||
|
||||
while (doc) {
|
||||
previousDoc = doc;
|
||||
doc = find(data, {longname: doc.memberof})[0];
|
||||
// Use index instead of database query
|
||||
doc = index.get(doc.memberof);
|
||||
|
||||
// prevent infinite loop that can be caused by duplicated module definitions
|
||||
if (previousDoc === doc) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user