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

@ -2,10 +2,9 @@
"use strict";
const { addError, addErrorDetailIf, getHtmlAttributeRe } =
require("../helpers");
const { filterByPredicate, filterByTypes, getHtmlTagInfo } =
require("../helpers/micromark.cjs");
const { addError, addErrorDetailIf, getHtmlAttributeRe } = require("../helpers");
const { filterByPredicate, filterByTypes, getHtmlTagInfo } = require("../helpers/micromark.cjs");
const { filterByTypesCached } = require("./cache");
// Regular expression for identifying HTML anchor names
const idRe = getHtmlAttributeRe("id");
@ -69,11 +68,10 @@ module.exports = {
"tags": [ "links" ],
"parser": "micromark",
"function": function MD051(params, onError) {
const micromarkTokens = params.parsers.micromark.tokens;
const fragments = new Map();
// Process headings
const headingTexts = filterByTypes(micromarkTokens, [ "atxHeadingText", "setextHeadingText" ]);
const headingTexts = filterByTypesCached([ "atxHeadingText", "setextHeadingText" ]);
for (const headingText of headingTexts) {
const fragment = convertHeadingToHTMLFragment(headingText);
if (fragment !== "#") {
@ -93,7 +91,7 @@ module.exports = {
}
// Process HTML anchors
for (const token of filterByTypes(micromarkTokens, [ "htmlText" ], true)) {
for (const token of filterByTypesCached([ "htmlText" ], true)) {
const htmlTagInfo = getHtmlTagInfo(token);
if (htmlTagInfo && !htmlTagInfo.close) {
const anchorMatch = idRe.exec(token.text) ||
@ -112,7 +110,7 @@ module.exports = {
[ "definition", "definitionDestinationString" ]
];
for (const [ parentType, definitionType ] of parentChilds) {
const links = filterByTypes(micromarkTokens, [ parentType ]);
const links = filterByTypesCached([ parentType ]);
for (const link of links) {
const definitions = filterByTypes(link.children, [ definitionType ]);
for (const definition of definitions) {