← Lessons

quiz vs the machine

Platinum1820

Frontend

The Immutability And Structural Sharing

Why copying without mutation stays cheap and enables fast change detection.

6 min read · advanced · beat Platinum to climb

What it is

Immutability means never changing existing data; you produce a new value instead. The worry is cost, but structural sharing keeps it cheap by reusing the parts of a structure that did not change.

How sharing works

When you update one field of a nested object, you create new objects only along the path from the root to that field. Every untouched branch is reused by reference. A deep tree update touches a logarithmic or constant number of nodes, not the whole structure.

  • Copy the changed path, share the rest.
  • Unchanged subtrees keep the same reference.
  • Persistent data structures generalize this idea.

Why it pays off

Because unchanged branches keep their identity, change detection becomes a cheap reference comparison. If the reference is the same, nothing inside changed, so memoized components and selectors can skip work.

Key idea

Structural sharing makes immutable updates cheap by copying only the changed path, which turns change detection into a fast reference comparison.

Check yourself

Answer to earn rating on the learn ladder.

1. What does structural sharing reuse during an update?

2. Why does immutability enable fast change detection?