Skip to content

Commit cbea149

Browse files
committed
fix: speed up templateHelper's getAncestor() by 500x
Backported from #2150, authored by @mrdoob.
1 parent 11a99cf commit cbea149

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

lib/jsdoc/util/templateHelper.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,26 @@ exports.getSignatureReturns = ({yields, returns}, cssClass) => {
805805
return returnTypes;
806806
};
807807

808+
// Cache for memberof index to speed up ancestor lookups
809+
let memberofIndex = null;
810+
811+
function buildMemberofIndex(data) {
812+
if (memberofIndex) {
813+
return memberofIndex;
814+
}
815+
816+
memberofIndex = new Map();
817+
const allDoclets = data().get();
818+
819+
for (const doclet of allDoclets) {
820+
if (doclet.longname) {
821+
memberofIndex.set(doclet.longname, doclet);
822+
}
823+
}
824+
825+
return memberofIndex;
826+
}
827+
808828
/**
809829
* Retrieve an ordered list of doclets for a symbol's ancestors.
810830
*
@@ -818,9 +838,13 @@ exports.getAncestors = (data, doclet) => {
818838
let doc = doclet;
819839
let previousDoc;
820840

841+
// Build index once for all lookups
842+
const index = buildMemberofIndex(data);
843+
821844
while (doc) {
822845
previousDoc = doc;
823-
doc = find(data, {longname: doc.memberof})[0];
846+
// Use index instead of database query
847+
doc = index.get(doc.memberof);
824848

825849
// prevent infinite loop that can be caused by duplicated module definitions
826850
if (previousDoc === doc) {

0 commit comments

Comments
 (0)