mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2026-01-05 23:48:49 +01:00
Reimplement micromark helper filterByPredicate to do fewer array manipulations for a ~6-7% elapsed time reduction.
This commit is contained in:
parent
24c97a54fb
commit
6a2b86753b
2 changed files with 49 additions and 18 deletions
|
|
@ -213,16 +213,33 @@ function micromarkParse(
|
|||
*/
|
||||
function filterByPredicate(tokens, allowed, transformChildren) {
|
||||
const result = [];
|
||||
const pending = [ ...tokens ];
|
||||
let token = null;
|
||||
while ((token = pending.shift())) {
|
||||
if (allowed(token)) {
|
||||
result.push(token);
|
||||
const queue = [
|
||||
{
|
||||
"array": tokens,
|
||||
"index": 0
|
||||
}
|
||||
if (token.children.length > 0) {
|
||||
const transformed =
|
||||
transformChildren ? transformChildren(token) : token.children;
|
||||
pending.unshift(...transformed);
|
||||
];
|
||||
while (queue.length > 0) {
|
||||
const current = queue[queue.length - 1];
|
||||
const { array, index } = current;
|
||||
if (index < array.length) {
|
||||
const token = array[current.index++];
|
||||
if (allowed(token)) {
|
||||
result.push(token);
|
||||
}
|
||||
const { children } = token;
|
||||
if (children.length > 0) {
|
||||
const transformed =
|
||||
transformChildren ? transformChildren(token) : children;
|
||||
queue.push(
|
||||
{
|
||||
"array": transformed,
|
||||
"index": 0
|
||||
}
|
||||
);
|
||||
}
|
||||
} else {
|
||||
queue.pop();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue