perf: unroll children
This commit is contained in:
parent
49b3fc3c46
commit
bfdd55e44a
|
@ -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');
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user