Immutability and referential transparency has many known benefits and ability for optimization. Several modern JavaScript libraries take advantage of this, and many more functional compile-to-JS languages.
All these types provide value equality for both ==
and ===
.
This proposal provides convenient syntax and semantics for immutable data structures and helpers to extend them. The actual implementation details of the extend operations are not covered by this spec but it's expected that engines optimize beyond just copying. E.g. using persistent data structures.
This is based upon the Typed Object proposal (Explainer).
Records are a new value type that represents an immutable object without a prototype.
const xObj = #{ x: 1 }; // frozen value type
const xyObj = #{ ...xObj, y: 2 }; // functional extension
Tuples are a new value type that represents an immutable array without a prototype and no .
const xy = #[ x, y ]; // frozen value type
const xyz = #[ ...xy, z ]; // functional extension
ImmutableMap is an immutable version of Map. Any mutable operation returns a new ImmutableMap instead of mutating the existing reference.
const a = new ImmutableMap([['x', 1], ['y', 2]]);
const b = a.set('x', 3);
a.get('x'); // 1
b.get('y'); // 3
ImmutableSet is an immutable version of Set. Any mutable operation returns a new ImmutableSet instead of mutating the existing reference.
const a = new ImmutableSet([1, 2]);
const b = a.add(3);
a.size; // 2
b.size; // 3
It is my intention to propose this as a Stage 0 proposal to ECMAScript 7.