Immutability
immutability
Code Snippets
Deep Clone with structuredClone
Deep cloning is no longer a `JSON.parse(JSON.stringify(x))` hack. Modern runtimes ship `structuredClone`, which handles cycles, Maps, Sets, typed arrays, and Date out of the box. This snippet shows the canonical built-in usage, the JSON fallback for legacy code paths and its real limits, and a hand-rolled recursive clone for when you need to skip specific keys or types. Pick the right tool and stop carrying lodash for one helper.
Freeze, Seal, and preventExtensions on Objects
JavaScript ships three integrity levels for objects (`Object.preventExtensions`, `Object.seal`, and `Object.freeze`) and they are easy to confuse because each silently relaxes one rule from the next. This snippet builds them up from least to most restrictive, shows the strict-mode behavior that turns silent failures into errors, and finishes with a recursive `deepFreeze` for nested config. Reach for these when you want a runtime guarantee that downstream code cannot mutate a shared object.
Question Banks
JavaScript Object Reference and Key Coercion Traces
Six traces covering object-key stringification, reference vs literal equality, `Object.create` prototype chains, and shallow-copy aliasing with arrays.
JavaScript Immutability and References Quiz
Lock down objects with `Object.freeze`, build read-only properties with `Object.defineProperty`, and reason about why React leans on immutable updates.
JavaScript Immutable Object Key Removal: Two Approaches Quiz
Two seeded approaches to remove an object key without mutation (spread + delete, reduce + filter), plus two companions on destructure-and-drop and a deep-clone caveat.
JavaScript Deep Object Comparison: Two Approaches Quiz
Diff the keys of two plain objects two ways (JSON.stringify shortcut and a recursive walker), plus two companions on circular references and a Map-based structural diff.
JavaScript Immutable Key Masking: Two Approaches Quiz
Mask the values of specified keys deeply, leaving the last three characters, two ways (recursive clone + mask and structuredClone + mask), plus two companions on path-based targeting and a JSON.stringify replacer variant.
System Design
Design Object Storage (S3)
Design an S3-style object storage service that stores trillions of immutable blobs ranging from 1 KB to 5 TB at eleven nines of durability and a fraction of the cost of triple replication. The interview centerpiece is the trio that makes this economical: erasure coding (typically 12 data shards plus 4 parity shards) instead of full replicas; a separate metadata service that maps object keys to chunk locations; and multi-part upload that lets a 5 TB object stream from many sources in parallel. We also cover the bucket/object namespace, lifecycle policies that move cold objects to colder tiers, immutability with versioning, pre-signed URLs for direct client transfer, and the move from eventual to strong read-after-write consistency that AWS shipped in 2020.
Community
How I Stopped Mutating Nested State in React
The bug we shipped: a deeply nested form state was being updated in place, and React refused to re-render. Here is the 25-line `setIn` helper I now reach for instead of Immer.
Pure Functions, Immutability, and the Trade-offs
Pure functions and immutability are not free. The honest accounting of the wins (testability, reasoning, parallelism) and the costs (allocation, ergonomics, language fit), and where the line falls in real code.
