DEV Community

John Peters
John Peters

Posted on • Updated on

Immutability Benefits

Please add to the conversation by writing a little bit about why you feel Immutability is such a good idea. Please!

I recently used an Angular component, whereby it contained a table of data, immutable data. I found out later that the only way to change any row being displayed was to make a copy of the row to be edited by the user, show it in another control, then to mutate the entire collection with the new value inserted once the user made the change.

In the past, we would just mutate the data and save it off to the data store. Never had issues with that in 25+ years.

I feel the work to get the immutable data to mutate was way more work than just changing the row that needed it.

Top comments (7)

Collapse
 
josemunoz profile image
José Muñoz

by having immutable data you can guarantee atomic access to you application's state. Not only does it means that you will eliminate race conditions where you read a value as it's being changed, you also make it safe to access the same resource from different processes.

Collapse
 
jwp profile image
John Peters

Jose, does "different processes" mean multiple asynchronous access to the same data?

Collapse
 
yawaramin profile image
Yawar Amin

That's right. In the browser/node this can happen due to async stuff like promises resolving at different times.

By the by, your table data structure may be the wrong one–it sounds like changing a row was not easy. You may want an immutable tree structure to store the table. It can make replacing rows very easy. See this great explanation of such a data structure: youtu.be/Wo0qiGPSV-s

Thread Thread
 
jwp profile image
John Peters

Thank you for your input Yawar!

You are saying, Immutability is good because of Asynchronous data concerns. The concern of multiple async jobs not knowing the state of the data.

Collapse
 
maroun_baydoun profile image
Maroun Baydoun

Immutability ensures quick change detections. If we are guranteed that any change will create a new version of our data, knowing that a change has happened becomes a matter of a simple reference compariason between the current and the previous data. Thus eleminating costly deep object comparaisons. This is particularly helpful with deep nested data structures that would be expenisve to traverse and compare.

Collapse
 
jwp profile image
John Peters

Yes, reference comparisons tell us there is a difference quickly.

Collapse
 
ahferroin7 profile image
Austin S. Hemmelgarn

The biggest benefit, IMO, is not something that can be achieved unless it's baked in at the language level.

Put simply, immutable data done right is potentially very efficient.

The BEAM runtime environment (used by Erlang, Elixir, and a few other languages) demonstrates this nicely. Because all data in BEAM is immutable (and they use reference counted garbage collection):

  • Sharing data between processes is trivially safe (in that the data won't just vanish or change while you're accessing it).
  • Local messaging is wicked fast. Sending a message between two processes on the same node just creates a new reference to the message data, so it's almost instant.
  • Many sequence operations are insanely fast, because the new sequence they generate references the old one wherever possible. Concatenation of sequences, manipulation of the first element in a list, etc.