diff --git a/desktop/plugins/public/layout/Inspector.tsx b/desktop/plugins/public/layout/Inspector.tsx index 06684aa75..cb3d0f0bf 100644 --- a/desktop/plugins/public/layout/Inspector.tsx +++ b/desktop/plugins/public/layout/Inspector.tsx @@ -341,20 +341,29 @@ export default class Inspector extends Component { } getElementLeaves(tree: ElementSelectorNode): Array { - return tree - ? Object.entries(tree).reduce( - ( - currLeafNode: Array, - [id, children]: [ElementID, ElementSelectorNode], - ): Array => - currLeafNode.concat( - Object.keys(children).length > 0 - ? this.getElementLeaves(children) - : [id], - ), - [], - ) - : []; + if (!tree) { + return []; + } + const leavesSet = new Set(); + + const treeIteratorStack: [ElementID, ElementSelectorNode][] = [ + ...Object.entries(tree), + ]; + while (treeIteratorStack.length) { + const [id, children] = treeIteratorStack.pop()!; + + if (leavesSet.has(id)) { + continue; + } + + if (Object.keys(children).length) { + treeIteratorStack.push(...Object.entries(children)); + } else { + leavesSet.add(id); + } + } + + return [...leavesSet]; } /// Return path from given tree structure and id if id is not null; otherwise return any path