dwyl / learn-react

"The possibilities are numerous once we decide to act and not react." ~ George Bernard Shaw
108 stars 22 forks source link

The case for Immutability #6

Open nelsonic opened 8 years ago

nelsonic commented 8 years ago
nelsonic commented 8 years ago

@rjmk can you please explain why immutability in JS leads to a performance improvement even as Memory Consumption _increases_?

ericelliott commented 8 years ago

Immutability using data structures such as those provided by Mori.js create trie structures which share common content between old and new versions of the data, so memory consumption may not be as bad as you think.

Immutability can also give you huge performance optimization opportunities, especially where it's necessary to compare changes against two different deeply nested trees (for example, the React virtual DOM vs actual DOM diff).

Using immutable data structures, the diff check is literally an identity check -- about as fast as a comparison gets.

Using mutable trees, you have to traverse the whole tree and compare each node just to find out whether or not anything has changed.

There are lots of other advantages to immutability, including enabling great features like unlimited undo/redo, history shuttles, and even time-travel debugging.

Immutability also adds a lot of guarantees that make understanding and debugging software easier, and completely eliminates entire classes of code and potential bugs.

iteles commented 8 years ago

@NataliaLKB You were telling me about your recent learnings on mutable and immutable objects on Saturday! Was just wondering if you have anything to add on what you've learnt :wink:

NataliaLKB commented 8 years ago

I agree with everything @ericelliott has said so far. Especially the last line.

Immutability also adds a lot of guarantees that make understanding and debugging software easier, and completely eliminates entire classes of code and potential bugs.

Even if memory consumption increases, it is entirely worth it for all the other benefits

rjmk commented 8 years ago

Agree with @ericelliott and @NataliaLKB. Especially Natalia's last comment.

Especially the last line.

Immutability also adds a lot of guarantees that make understanding and debugging software easier, and completely eliminates entire classes of code and potential bugs.

Even if memory consumption increases, it is entirely worth it for all the other benefits