You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Converts an array of items with ids and parent ids to a nested tree in a performant `O(n)` way. Runs in browsers and node.
13
+
Converts an array of items with ids and parent ids to a nested tree in a performant way (time complexity `O(n)`). Runs in browsers and node.
14
14
15
15
## Why another package
16
16
17
-
Other packages have stricter assumptions or are not as performant, as they often use multiple loops or recursion. For example:
17
+
Other packages have stricter assumptions or are not as performant, as they often use nested loops or recursion. For example:
18
18
19
19
[o-unflatten](https://www.npmjs.com/package/o-unflatten) requires the input to be ordered such that parent nodes always come before their children.
20
-
[array-to-tree](https://www.npmjs.com/package/array-to-tree) uses two loops (runs with `O(2n)`), one for grouping all items by their parents, and one for creating the tree.
21
-
[un-flatten-tree](https://www.npmjs.com/package/un-flatten-tree) uses 2 nested loops (runs even worse with `O(n^2)`).
This implementation does not require any order of items in the input array and focuses on runtime performance. By only using a single loop (runs with`O(n)`). It was inspired by [this discussion](http://stackoverflow.com/questions/444296/how-to-efficiently-build-a-tree-from-a-flat-structure) on StackOverflow.
22
+
This implementation does not require any order of items in the input array and focuses on runtime performance. It uses an index and a single loop (time complexity`O(n)`). It was inspired by [this discussion](http://stackoverflow.com/questions/444296/how-to-efficiently-build-a-tree-from-a-flat-structure) on StackOverflow.
0 commit comments