DEV Community

Discussion on: Immutability Benefits

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.