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

@ -3,7 +3,8 @@
"use strict";
const { addErrorContext } = require("../helpers/helpers");
const { filterByTypes, getHeadingStyle } = require("../helpers/micromark.cjs");
const { getHeadingStyle } = require("../helpers/micromark.cjs");
const { filterByTypesCached } = require("./cache");
/**
* Validate heading sequence and whitespace length at start or end.
@ -55,10 +56,8 @@ module.exports = [
"tags": [ "headings", "atx", "spaces" ],
"parser": "micromark",
"function": function MD019(params, onError) {
const atxHeadings = filterByTypes(
params.parsers.micromark.tokens,
[ "atxHeading" ]
).filter((heading) => getHeadingStyle(heading) === "atx");
const atxHeadings = filterByTypesCached([ "atxHeading" ])
.filter((heading) => getHeadingStyle(heading) === "atx");
for (const atxHeading of atxHeadings) {
validateHeadingSpaces(onError, atxHeading, 1);
}
@ -70,10 +69,8 @@ module.exports = [
"tags": [ "headings", "atx_closed", "spaces" ],
"parser": "micromark",
"function": function MD021(params, onError) {
const atxClosedHeadings = filterByTypes(
params.parsers.micromark.tokens,
[ "atxHeading" ]
).filter((heading) => getHeadingStyle(heading) === "atx_closed");
const atxClosedHeadings = filterByTypesCached([ "atxHeading" ])
.filter((heading) => getHeadingStyle(heading) === "atx_closed");
for (const atxClosedHeading of atxClosedHeadings) {
validateHeadingSpaces(onError, atxClosedHeading, 1);
validateHeadingSpaces(onError, atxClosedHeading, -1);