How does changing the node without calling SKIP cause performance issues? #107
-
I have been reading the docs over at https://unifiedjs.com/learn/recipe/remove-node/#replacing-a-node-with-its-children. It says if you remove a node or replace it's content, it would cause performance issues because you are still transversing the tree. I don't understand how it causes performance issues. I tried replace a node directly with parent[index] = new node contents and it works perfectly. From what I understand, each plugin takes a tree modifies it and returns it to the next plugin so without calling the SKIP, won't the next plugin realise there's nothing there??? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Welcome @lubiah 👋 The visitor does a depth first traversal. Meaning if you modify the current node, that will effect the traversal of its children. Some example of how this can cause bugs or performance problems (there are many more)
The recommendations in the recipe are there as defensive measures, to raise awareness, a reminder to be thoughtful in how you modify the tree, and considerate of how that could impact performance. |
Beta Was this translation helpful? Give feedback.
Welcome @lubiah 👋
The potential issue is within the plugin where the transformation is happening.
The visitor does a depth first traversal.
https://github.com/syntax-tree/unist-util-visit-parents#visitparentstree-test-visitor-reverse
https://en.m.wikipedia.org/wiki/Tree_traversal
Meaning if you modify the current node, that will effect the traversal of its children.
Some example of how this can cause bugs or performance problems (there are many more)