Skip to content
Onsite

Deep Copy

MediumP1Coding

Implement deepCopy(value).

Return a deep copy of value. It must handle:

  • primitives (returned as they are)
  • plain objects and arrays, at any depth
  • Date, Map, Set and RegExp
  • circular references: a copy of a cyclic structure is cyclic in the same way, and shared references stay shared (if two properties point to the same object, the copies point to one copy)

Functions can be returned as they are. Do not use structuredClone or JSON.parse(JSON.stringify(...)).

Example 1

Input
const a = { x: [1, { y: 2 }] }; const b = deepCopy(a);
Output
b equals a, but b !== a and b.x[1] !== a.x[1]

Constraints

  • Nesting depth up to 1000
  • Only own enumerable string keys need to be copied; prototypes of plain objects can be Object.prototype

Hints

Run checks the 3 visible tests, Submit all 9.