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, allPunctuation } = require("../helpers");
const { filterByTypes, matchAndGetTokensByType } = require("../helpers/micromark.cjs");
const { matchAndGetTokensByType } = require("../helpers/micromark.cjs");
const { filterByTypesCached } = require("./cache");
/** @typedef {import("../helpers/micromark.cjs").TokenType} TokenType */
/** @type {Map<TokenType, TokenType[]>} */
@ -24,7 +25,7 @@ module.exports = {
punctuation = String((punctuation === undefined) ? allPunctuation : punctuation);
const punctuationRe = new RegExp("[" + punctuation + "]$");
const paragraphTokens =
filterByTypes(params.parsers.micromark.tokens, [ "paragraph" ]).
filterByTypesCached([ "paragraph" ]).
filter((token) =>
(token.parent?.type === "content") && !token.parent?.parent && (token.children.length === 1)
);