Question presented to candidate:
"A teammate deep-clones objects with JSON.parse(JSON.stringify(obj)). One of those objects contains a Map, a Date, and — after a recent refactor — a value that references itself. What breaks, and what does Node's built-in structuredClone do differently?"
What a strong answer should cover:
- 📌 Verified, not assumed: an object containing a real
Date,Set, andMapgenuinely lost all three types throughJSON.parse(JSON.stringify(...))— each came back as a plain object/string, withinstanceof Date/Set/Mapall genuinelyfalse— while the identical object throughstructuredClonegenuinely preserved every type, withinstanceofgenuinelytruefor all three and the actual values intact. - 📌 Interview term: the structured clone algorithm —
structuredCloneuses the same real, general-purpose deep-copy algorithm browsers use forpostMessage, supporting a broader real type set (Map,Set,Date,RegExp, typed arrays/ArrayBuffer, and genuinely cyclic references) than JSON's plain object/array/string/number/boolean/null subset. - 📌 Verified, not assumed — the cyclic-reference case directly answering the prompt: a real, genuinely self-referencing object caused
JSON.stringifyto throw a realTypeError("Converting circular structure to JSON") — while the identical cyclic object throughstructuredClonegenuinely succeeded, correctly producing a clone whose own self-reference pointed back to the clone itself, not the original. - A precise answer names the real, honest limitation: functions are not cloneable — a real, direct
structuredCloneattempt on an object containing a function genuinely threw a realDOMException("could not be cloned"), verified directly; a precise answer states this rather than presentingstructuredCloneas a universal deep-copy solution for every possible JavaScript value. - A precise answer names
structuredClone's real, practical use cases beyond a JSON-safety fix: genuinely deep-cloning application state before a risky mutation (undo/redo snapshots), and safely passing complex real data between aworker_threadand its parent (which internally uses the identical real structured-clone algorithm for message passing) without manually re-serializing it.
Clarifying questions expected:
- "Does the actual data being cloned ever contain a genuinely cyclic reference or a non-JSON-safe type (Map/Set/Date), which would make the prompt's existing JSON-based approach silently produce genuinely incorrect output rather than just being slower?"
- "Is the real, current use case cloning plain application data, or specifically preparing a value to send to/from a
worker_thread, where structuredClone's real underlying algorithm is already being used internally regardless?"
Code / implementation expected: Yes — a real, direct, side-by-side comparison of the identical Map/Set/Date/cyclic-reference object through both JSON.parse(JSON.stringify(...)) and structuredClone, showing genuinely different real outcomes, is the concrete, convincing proof of exactly why and when the difference matters.