06 · Frontend Coding Patterns
hardcodingjavascript~15 min

Deep Clone with Circular References

Objective: Walk object graphs safely using a WeakMap of already-seen nodes.

Implement `deepClone(value)` for plain objects, arrays and Dates, and make it survive circular references.

  • Primitives are returned as-is.
  • Arrays, plain objects and Dates are copied recursively; the copy shares no references.
  • Circular references are preserved in the copy (no infinite recursion).
  • Repeated references to the same object map to the same clone.
JavaScript

⌘/Ctrl + Enter to run tests · Esc then Tab leaves the editor

Output
> Nothing has run yet.
Interview follow-up questions
  • When is structuredClone enough, and what can it not copy?
  • What happens to functions, Maps and class instances in your version?
  • Why does JSON.stringify fail on circular data?