From 16512bba250cafc7bab41c9295db9665bd5ff5bd Mon Sep 17 00:00:00 2001 From: David Anson Date: Wed, 23 Oct 2024 22:05:44 -0700 Subject: [PATCH] Reorder operations in getDescendantsByType helper for ~1% runtime reduction. --- demo/markdownlint-browser.js | 5 ++--- helpers/micromark-helpers.cjs | 5 ++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/demo/markdownlint-browser.js b/demo/markdownlint-browser.js index c52e419bc..4713f8d0a 100644 --- a/demo/markdownlint-browser.js +++ b/demo/markdownlint-browser.js @@ -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; } diff --git a/helpers/micromark-helpers.cjs b/helpers/micromark-helpers.cjs index eb868fe2f..6b4ff09c1 100644 --- a/helpers/micromark-helpers.cjs +++ b/helpers/micromark-helpers.cjs @@ -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; }