ImmutableJS is actually quite efficient. If we take an example:
var x = {
Foo: 1,
Bar: { Baz: 2 }
Qux: { AnotherVal: 3 }
}
If the above object is made immutable and you modify the value of the Baz
property, what you would get is:
var y = x.setIn('/Bar/Baz', 3);
y !== x; // Different object instance
y.Bar !== x.Bar // As the Baz property was changed, the Bar object is a diff instance
y.Qux === x.Qux // Qux is the same object instance
This creates some really cool performance improvements for deep object models, where you only need to copy value types on objects on the path to the root. The larger the object model and the smaller the changes you make, the better the memory and CPU performance of the immutable data structure as they end up sharing lots of objects.
As the other answers have said, if you contrast this to trying to provide the same guarantees by defensively copying x
before passing it into a function that could manipulate it then the performance is significantly better.