JavaScriptMedium
What is the difference between shallow and deep freeze?
Object.freeze is a shallow freeze: it locks only the object's own top-level properties, so nested objects stay mutable; a deep freeze recursively freezes every nested object and array, making the whole structure immutable.
Shallow freeze
- Built in:
Object.freeze(obj). - Top-level properties cannot be added, removed or reassigned.
- But
frozen.settings.theme = "dark"still works ifsettingsis an object, because only the reference to it is frozen, not the object itself.
Deep freeze
- Not built in; implemented with recursion over
Object.values(orReflect.ownKeysto include symbol keys). - Must avoid infinite loops on circular references (skip objects that are already frozen).
- Should skip things that should not be frozen or cannot be meaningfully frozen: typed arrays with elements throw when frozen, and freezing objects you do not own (like DOM nodes or library instances) can break them.
When to use which
- Shallow is enough for flat constants (
ROLES,HTTP_STATUS). - Deep for configuration trees, test fixtures, and in development to catch accidental mutation of state (Redux Toolkit and Immer freeze state in development for exactly this reason).
Costs
Deep freezing large structures takes time and is usually skipped in production builds. TypeScript's Readonly/as const gives compile-time immutability without runtime cost; combine both where runtime safety matters.
Note on Maps and Sets
Freezing a Map or Set does not stop .set()/.add(), because entries are internal state, not properties. Wrap them or use immutable data structures if you need that.
Code example
js
1"use strict";23const config = {4 name: "app",5 db: { host: "localhost", ports: [5432, 5433] },6};78// Shallow9const shallow = Object.freeze(structuredClone(config));10shallow.db.host = "changed"; // allowed!11shallow.db.ports.push(9999); // allowed!12console.log(shallow.db); // { host: "changed", ports: [5432, 5433, 9999] }13try {14 shallow.name = "x";15} catch (e) {16 console.log("top level is frozen:", e.name); // TypeError17}1819// Deep freeze: recursive, cycle-safe20function deepFreeze(obj) {21 if (obj === null || typeof obj !== "object" || Object.isFrozen(obj) || ArrayBuffer.isView(obj)) return obj;22 Object.freeze(obj); // freeze first, so cycles stop here23 for (const key of Reflect.ownKeys(obj)) deepFreeze(obj[key]);24 return obj;25}2627const deep = deepFreeze(structuredClone(config));28console.log(Object.isFrozen(deep.db), Object.isFrozen(deep.db.ports)); // true true29for (const attempt of [() => (deep.db.host = "changed"), () => deep.db.ports.push(1)]) {30 try {31 attempt();32 } catch (e) {33 console.log("deep frozen:", e.name); // TypeError (x2)34 }35}3637// Circular structures are handled38const cyclic = { a: {} };39cyclic.a.parent = cyclic;40console.log(Object.isFrozen(deepFreeze(cyclic).a)); // true
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.