fix: speed up templateHelper's getAncestor() by 500x

Backported from #2150, authored by @mrdoob.
This commit is contained in:
Jeff Williams 2025-10-07 19:54:45 -07:00
parent 11a99cfb5d
commit cbea149ae8
No known key found for this signature in database

View File

@ -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) {