perf: unroll children

This commit is contained in:
cha0s 2024-07-20 05:38:44 -05:00
parent 49b3fc3c46
commit bfdd55e44a

View File

@ -37,15 +37,19 @@ export default function traverse(node, visitor) {
throw new Error(`node type ${node.type} not traversable. (${Object.keys(node).join(', ')})`);
}
visitor(node, 'enter');
const path = TRAVERSAL_PATH[node.type];
const children = [];
for (const key of path) {
children.push(...(Array.isArray(node[key]) ? node[key] : [node[key]]));
}
for (const child of children) {
for (const key of TRAVERSAL_PATH[node.type]) {
if (Array.isArray(node[key])) {
for (const child of node[key]) {
if (child) {
traverse(child, visitor);
}
}
}
else {
if (node[key]) {
traverse(node[key], visitor);
}
}
}
visitor(node, 'exit');
}