From 2c83dd570066dbdcec32be0da4ad943b3d57e506 Mon Sep 17 00:00:00 2001 From: "ruben.vargas" Date: Wed, 6 Feb 2019 20:27:56 -0600 Subject: [PATCH] Optimize tree walk Signed-off-by: ruben.vargas --- packages/jaeger-ui/src/utils/TreeNode.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/jaeger-ui/src/utils/TreeNode.js b/packages/jaeger-ui/src/utils/TreeNode.js index 5545fd0f67..b3ef63c8cc 100644 --- a/packages/jaeger-ui/src/utils/TreeNode.js +++ b/packages/jaeger-ui/src/utils/TreeNode.js @@ -83,7 +83,18 @@ export default class TreeNode { } walk(fn, depth = 0) { - TreeNode.iterFunction(fn, depth)(this); - this.children.forEach(child => child.walk(fn, depth + 1)); + const nodeStack = []; + let actualDepth = depth; + nodeStack.push({ node: this, depth: actualDepth }); + while (nodeStack.length) { + const { node, depth: nodeDepth } = nodeStack.pop(); + fn(node.value, node, nodeDepth); + actualDepth = nodeDepth + 1; + let i = node.children.length - 1; + while (i >= 0) { + nodeStack.push({ node: node.children[i], depth: actualDepth }); + i--; + } + } } }