From bfdd55e44a84748d6a7f2af74fade92590cb0333 Mon Sep 17 00:00:00 2001 From: cha0s Date: Sat, 20 Jul 2024 05:38:44 -0500 Subject: [PATCH] perf: unroll children --- app/astride/traverse.js | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/app/astride/traverse.js b/app/astride/traverse.js index 448659c..9bbe352 100644 --- a/app/astride/traverse.js +++ b/app/astride/traverse.js @@ -37,14 +37,18 @@ 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) { - if (child) { - traverse(child, visitor); + 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');