Deep Clone
JS Functions
Medium
15 min
objects
recursion
lodash
Implement deepClone(value). It returns a deep copy of value: a structurally equal value in which no object or array is shared with the original, so mutating the copy at any depth never affects the original (and vice versa).
const original = { user: { name: 'Ada', tags: ['math'] }, count: 1 };
const copy = deepClone(original);
copy.user.tags.push('code');
original.user.tags; // ['math'] (unchanged)
copy.user === original.user; // false
Signature
deepClone(value)
Scope
The input is JSON-like data:
- primitives:
string,number(includingNaN,Infinity),boolean,null,undefined,bigint,symbol; - plain objects and arrays, nested to any depth.
Requirements
- Primitives are returned as they are.
- Arrays are cloned as arrays (
Array.isArray(copy)istrue), preserving length and order. - Objects are cloned as new plain objects with all own enumerable string keys.
- Values that
JSON.stringifywould lose must survive:undefinedvalues stay present as keys,NaN/Infinitystay numbers,-0stays-0. - No cycles,
Date,Map,Set, class instances or functions in the input (see thedeep-clone-iiquestion for those). - Do not use
structuredCloneorJSON.parse(JSON.stringify(...)). Write the recursion yourself.
Follow-up
- What does
JSON.parse(JSON.stringify(x))get wrong? - What does
structuredClonesupport, and what does it throw on? - How would you handle circular references?
Spotted a mistake or a rights concern? Report an issue