From 3fa7cc42294976f7a4d98dfbd15ac3a91a390fcd Mon Sep 17 00:00:00 2001 From: gregdolley Date: Sun, 27 Nov 2022 21:03:53 -0800 Subject: [PATCH] Improvement to default iterator generator function There's no need to call reverse() on the "next" array if you insert elements into the array in reverse order via unshift() (in the inner for loop) instead of push() (how it was being done originally). --- src/hierarchy/iterator.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/hierarchy/iterator.js b/src/hierarchy/iterator.js index 7e06b620..3a17c6fd 100644 --- a/src/hierarchy/iterator.js +++ b/src/hierarchy/iterator.js @@ -1,12 +1,12 @@ export default function*() { - var node = this, current, next = [node], children, i, n; + let node = this, current, next = [node], children; do { - current = next.reverse(), next = []; + current = next, next = []; while (node = current.pop()) { yield node; if (children = node.children) { - for (i = 0, n = children.length; i < n; ++i) { - next.push(children[i]); + for (let i = 0; i < children.length; ++i) { + next.unshift(children[i]); } } }