CodePark/ questions / Equality & copying
START PRACTICINGPRACTICE
Frontend question · JavaScript › Equality & copying

Explain shallow vs deep copy. What are the ways to deep copy, and what are the pitfalls of JSON.parse(JSON.stringify(obj))?

A shallow copy makes a new top-level object, but nested objects are still shared. A deep copy copies every level, so nothing is shared.

Shallow copy:

  • Spread ({ ...obj }, [...arr]), Object.assign, Array.from and slice are all shallow.
  • If you change a nested property through the copy, the original changes too.

Deep copy with structuredClone(obj): this is the modern built-in.

  • It handles nested objects, cyclic references (objects that point back to themselves), Date, Map, Set, typed arrays and more.
  • It throws an error on functions and DOM nodes.
  • It does not keep class prototypes, getters or symbol keys.

Pitfalls of JSON.parse(JSON.stringify(obj)):

  • It drops undefined, functions and symbol values.
  • It turns Date into a string.
  • It turns NaN and Infinity into null.
  • It turns Map and Set into {}.
  • It throws on cycles and on BigInt.
  • It loses prototypes.
  • A shallow copy makes a new top level, but nested objects are still shared.
  • Spread, Object.assign and slice make shallow copies.
  • structuredClone makes deep copies and supports cycles, Date, Map and Set.
  • structuredClone throws on functions and loses prototypes.
  • The JSON round-trip loses undefined and functions, turns dates into strings, and throws on cycles.