Q&A-1Explain values against references and how it changes a production decision.
Answer
The 90-second answer. A binding holds a value. For primitives that value is the data itself; for objects, arrays and functions it is a reference to a heap object. Assignment, argument passing and === all operate on that value, so passing an object passes the reference by value: the callee can mutate the object the caller sees, but cannot rebind the caller's variable. The production consequence is aliasing: two parts of the system that each believe they own a value are sharing one, and a write in one shows up in the other.
// Node 20, strict mode
function mutate(o) { o.n = 2; }
function rebind(o) { o = { n: 3 }; }
const box = { n: 1 };
mutate(box); // box.n === 2
rebind(box); // box.n === 2 — the parameter was rebound, not the caller's bindingA shallow copy stops at the top level, which is where most of the surprises live:
const base = { n: 1, tags: ['a'] };
const shallow = { ...base };
shallow.n = 2; // base.n still 1
shallow.tags.push('b'); // base.tags is now ['a', 'b'] — the array was sharedFailure modes, and how you catch each.
- State mutation that never re-renders.
items.push(x); setItems(items)passes the same reference, React'sObject.ischeck bails out, and the UI stays stale while the store's array is correct. Detect: React DevTools Profiler shows no commit for the dispatch. - Identity-keyed cache that never hits.
cache.set(req, result)wherereqis rebuilt per call gives a 0% hit rate and unbounded growth. Detect: cache size climbs linearly with request count while the hit-rate metric sits flat at 0. structuredCloneon a class instance. It returns a plain object, sostructuredClone(new Conn()).ping()throwsTypeError: ... is not a function; it also throwsDataCloneErroron functions. Detect: a unit test that clones a fixture and calls a method on it.Object.freezeis shallow.Object.freeze({ db: { pool: 10 } })still allowscfg.db.pool = 20: silently in sloppy mode,TypeErrorin strict mode. Detect: strict mode plus a test that writes to a nested field.
Trade-offs.
| Approach | Cost | Wins when | Loses when |
|---|---|---|---|
| Mutate in place | O(1) memory | hot loop over a buffer you own | the value crosses a boundary you don't control |
Shallow copy ({...o}, Object.assign) | O(top-level keys) | flat config/state objects, React updates | nested objects are still shared |
structuredClone (Node 17+, evergreen browsers) | O(total nodes) time and memory | worker/thread boundary, cycles, Map/Set/Date | functions, class instances (prototype lost), large graphs |
Object.freeze | no copy | dev-time guard on a constant | shallow only; nested writes still land |
Worked arithmetic, hypothetical but the shape is real: a 50k-node graph cloned per request at 1k rps allocates 50M nodes per second. That is the case for cloning once at startup and freezing, not per request.
The decision. Name the contract in the function name, mutateX versus withX, and copy at the boundary where ownership changes: worker postMessage, cache insert, setState. Freeze config at startup. Reach for structuredClone when the graph has cycles or Map/Set; write a hand-rolled copy when it has methods you need to keep.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.