perf: unroll children

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

View File

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