Reorder operations in getDescendantsByType helper for ~1% runtime reduction.

This commit is contained in:
David Anson 2024-10-23 22:05:44 -07:00
parent e6e799d145
commit 16512bba25
2 changed files with 4 additions and 6 deletions

View file

@ -161,9 +161,8 @@ function getBlockQuotePrefixText(tokens, lineNumber, count = 1) {
function getDescendantsByType(parent, typePath) {
let tokens = Array.isArray(parent) ? parent : [ parent ];
for (const type of typePath) {
tokens = tokens
.flatMap((t) => t.children)
.filter((t) => Array.isArray(type) ? type.includes(t.type) : (type === t.type));
const predicate = (token) => Array.isArray(type) ? type.includes(token.type) : (type === token.type);
tokens = tokens.flatMap((t) => t.children.filter(predicate));
}
return tokens;
}