Skip to content

Commit

Permalink
Reorder operations in getDescendantsByType helper for ~1% runtime red…
Browse files Browse the repository at this point in the history
…uction.
  • Loading branch information
DavidAnson committed Oct 24, 2024
1 parent e6e799d commit 16512bb
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
5 changes: 2 additions & 3 deletions demo/markdownlint-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -843,9 +843,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;
}
Expand Down
5 changes: 2 additions & 3 deletions helpers/micromark-helpers.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 16512bb

Please sign in to comment.