What are immutability patterns?
Immutability patterns are techniques for updating data by creating new versions instead of mutating existing ones, such as spread updates, non-mutating array methods, structural sharing, freezing, and helper libraries like Immer.
Core patterns
- Object update:
{ ...obj, key: value }. - Nested update: copy each level along the changed path:
{ ...state, user: { ...state.user, address: { ...state.user.address, city } } }. - Array add/remove/update:
[...list, item],list.filter(i => i.id !== id),list.map(i => i.id === id ? { ...i, done: true } : i). - ES2023 copies:
toSorted,toReversed,toSpliced,with(index, value). - Remove a key:
const { removed, ...rest } = obj. - Maps/Sets: create a new one:
new Map(old).set(k, v).
Structural sharing
Only the changed path is copied; everything else is reused by reference. This keeps updates cheap and lets libraries detect changes with ===, which is how React, Redux and memoisation decide what to re-render.
Enforcing immutability
Object.freeze(shallow) or a recursive deep freeze in development.- TypeScript's
readonlyandReadonly<T>,as const.
Immer
Write "mutating" code against a draft; Immer produces the immutable result with structural sharing. Redux Toolkit uses it internally. It shines with deeply nested state.
Normalise deep state
If updates require copying five levels deep, flatten the data (store entities by id in maps) so updates touch one level.
Code example
1const state = {2 user: { name: "Ada", address: { city: "London", zip: "N1" } },3 todos: [{ id: 1, title: "Write", done: false }, { id: 2, title: "Ship", done: false }],4};56// Nested update: copy only along the changed path7const moved = {8 ...state,9 user: { ...state.user, address: { ...state.user.address, city: "Paris" } },10};11console.log(state.user.address.city, moved.user.address.city); // London Paris12console.log(moved.todos === state.todos); // true - untouched parts are shared1314// Array patterns15const toggled = state.todos.map((t) => (t.id === 2 ? { ...t, done: true } : t));16const removed = state.todos.filter((t) => t.id !== 1);17const added = [...state.todos, { id: 3, title: "Celebrate", done: false }];18console.log(toggled[1].done, removed.length, added.length, state.todos[1].done); // true 1 3 false1920// Remove a key immutably21const { zip, ...addressWithoutZip } = state.user.address;22console.log(addressWithoutZip); // { city: "London" }2324// Deep freeze in development to catch accidental mutation25const deepFreeze = (obj) => {26 Object.values(obj).forEach((v) => typeof v === "object" && v !== null && deepFreeze(v));27 return Object.freeze(obj);28};29"use strict";30const frozen = deepFreeze(structuredClone(state));31console.log(Object.isFrozen(frozen.user.address)); // true3233// Normalised state: entities by id make updates shallow34const byId = { 1: { title: "Write" }, 2: { title: "Ship" } };35const updated = { ...byId, 2: { ...byId[2], title: "Ship it" } };36console.log(updated[2].title, byId[2].title); // Ship it Ship
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the JavaScript set.