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 { addErrorDetailIf } = require("../helpers");
const { filterByTypes, getTokenParentOfType } = require("../helpers/micromark.cjs");
const { getTokenParentOfType } = require("../helpers/micromark.cjs");
const { filterByTypesCached } = require("./cache");
// eslint-disable-next-line jsdoc/valid-types
/** @type import("markdownlint-micromark").TokenType[] */
@ -27,10 +28,7 @@ module.exports = {
const startIndent = Number(params.config.start_indent || indent);
const unorderedListNesting = new Map();
let lastBlockQuotePrefix = null;
const tokens = filterByTypes(
params.parsers.micromark.tokens,
unorderedListTypes
);
const tokens = filterByTypesCached(unorderedListTypes);
for (const token of tokens) {
const { endColumn, parent, startColumn, startLine, type } = token;
if (type === "blockQuotePrefix") {
@ -40,6 +38,7 @@ module.exports = {
/** @type {import("../helpers/micromark.cjs").Token | null} */
let current = token;
while (
// @ts-ignore
(current = getTokenParentOfType(current, unorderedParentTypes))
) {
if (current.type === "listUnordered") {