0

Lee byron makes this point in the video, but I can't seem to find the part where he explains this.

https://www.youtube.com/watch?v=I7IdS-PbEgI&t=1604s

Is this because when you update a node you have traverse log(n) to get to the node. With an immutable structure and it must copy worst-case n nodes... That is as far as I get in my thinking.

1 Answers1

0

If you would attempt to create an immutable list the simple way, the obvious solution would be to copy the whole list into a new list and exchange that single item. So a larger list would take longer to copy, right? The result would at least be O(n).

Immutable.js on the other hand uses a trie (see wikipedia) graph, which allows to reuse most of the structure while making sure, that existing references are not mutated.

Simply said, you create a new tree structure and create new branches for modified parts. When one of the branches is unchanged, the tree can just link to the original structure instead of copying.

The immutable.js documentation starts with two links to long descriptions, especially the one about vector tries is nice:

These data structures are highly efficient on modern JavaScript VMs by using structural sharing via hash maps tries and vector tries as popularized by Clojure and Scala, minimizing the need to copy or cache data.

If you want to know more the details, you might want to take a look on the question about How Immutability is Implemented too.

dube
  • 4,898
  • 2
  • 23
  • 41