Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why people stress to use Immutable structures in javascript ? #46

Closed
asvny opened this issue Jul 21, 2015 · 2 comments
Closed

Why people stress to use Immutable structures in javascript ? #46

asvny opened this issue Jul 21, 2015 · 2 comments

Comments

@asvny
Copy link

asvny commented Jul 21, 2015

No description provided.

@passy
Copy link
Owner

passy commented Jul 22, 2015

Great question, but I won't be able to give a good answer compared to what's already out there.

In a nutshell, it's about containing complexity. You don't really need to worry about this if you build a one-off demo on Codepen or develop a throw-away prototype in one or two days. But as soon as you start thinking about maintainability of a project or getting other people to work at it, managing complexity is one of the most important things to concern yourself with.

Computers are inherently mutable. In the end, you'll be writing to the same bits of memory. But by the same logic, we'd have to keep writing our web apps in assembler. Luckily, there are powerful abstractions that make our lives easier. Think of functions instead of gotos and direct jumps. Another one of those is immutable data structures. Immutable data structures are not about avoiding change, but about managing change and making programs easier to reason about. Mutability makes things hard. If you've ever made a defensive deep-copy of an object before passing it to a function, you've hit a case where immutability would have helped you - and would have even been faster. Another place where they are faster is for comparison and this is precisely why people keep stressing to use them in JavaScript. If keep your state that is used to render a UI in one nested mutable data structure, it's expensive to verify whether it has changed since the last frame, because you have to walk down the tree and compare each individual leaf node (or at least up to the first change). With immutable data structures this is a simple referential comparison. If the top-level variable points to the same location in memory, you know that it is in fact the same object.

But I won't do this topic justice in this answer. Luckily people way smarter than me have already talked about this - a lot.

This one here is a great introduction:
screenshot 2015-07-22 12 53 34

And I would be remiss not to point you to one of the many amazing talks @Swanodette has given on this topic. Just pick any one:

@passy passy closed this as completed Jul 22, 2015
@asvny
Copy link
Author

asvny commented Jul 23, 2015

Thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants