fix(gatsby): fix an infinite loop in node child collection #17078
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Currently there are cases where a cyclic node graph can cause findChildrenRecursively to hit the limit of the stack. Also, recursive functions are notoriously slow in JS.
I've replaced this with a non-recursive implementation of a pre-order depth first search tree traversal, and also made it keep track of traversed nodes. This will prevent duplicate nodes being found, and also prevent infinite loops with cyclic node trees.
Performance wise this should theoretically be the same or possibly slightly better - I have not done a performance benchmark.Edit: Just made a change and benchmarked it, this is 3x faster than before.Correct me if I'm wrong - but renaming this method is fine as it is not exported, and all local usages have been fixed.