Cache all top-level calls to filterByTypes (~7% runtime reduction).

This commit is contained in:
David Anson 2024-08-24 22:05:16 -07:00
parent 85e704f32a
commit dfcb4529f3
47 changed files with 427 additions and 481 deletions

View file

@ -4,6 +4,7 @@
const { addError, getHtmlAttributeRe, nextLinesRe } = require("../helpers");
const { filterByTypes, getHtmlTagInfo } = require("../helpers/micromark.cjs");
const { filterByTypesCached } = require("./cache");
const altRe = getHtmlAttributeRe("alt");
@ -15,13 +16,8 @@ module.exports = {
"tags": [ "accessibility", "images" ],
"parser": "micromark",
"function": function MD045(params, onError) {
const micromarkTokens = params.parsers.micromark.tokens;
// Process Markdown images
const images = filterByTypes(
micromarkTokens,
[ "image" ]
);
const images = filterByTypesCached([ "image" ]);
for (const image of images) {
const labelTexts = filterByTypes(image.children, [ "labelText" ]);
if (labelTexts.some((labelText) => labelText.text.length === 0)) {
@ -39,11 +35,7 @@ module.exports = {
}
// Process HTML images
const htmlTexts = filterByTypes(
micromarkTokens,
[ "htmlText" ],
true
);
const htmlTexts = filterByTypesCached([ "htmlText" ], true);
for (const htmlText of htmlTexts) {
const { startColumn, startLine, text } = htmlText;
const htmlTagInfo = getHtmlTagInfo(htmlText);