What is the most efficient way to deep clone an object in JavaScript?

What is the most efficient way to clone a JavaScript object? I’ve seen obj = eval(uneval(o)); being used, but that’s non-standard and only supported by Firefox.

I’ve done things like obj = JSON.parse(JSON.stringify(o)); but question the efficiency.

I’ve also seen recursive copying functions with various flaws.
I’m surprised no canonical solution exists.

2019-June note: This was originally a reply to another answer, not a proper response to this question. No idea why it was picked as the right answer. But since the upvotes snowballed and it’s by far the #1 answer to this question, it will summarize the solutions as a wiki answer.

Native deep cloning

It’s called “structured cloning”, works experimentally in Node 11 and later, and hopefully will land in browsers. See this answer for more details.

Fast cloning with data loss - JSON.parse/stringify

If you do not use Date s, functions, undefined , Infinity , RegExps, Maps, Sets, Blobs, FileLists, ImageDatas, sparse Arrays, Typed Arrays or other complex types within your object, a very simple one liner to deep clone an object is:


const a = {
  string: 'string',
  number: 123,
  bool: false,
  nul: null,
  date: new Date(),  // stringified
  undef: undefined,  // lost
  inf: Infinity,  // forced to 'null'
  re: /.*/,  // lost
console.log(typeof a.date);  // Date object
const clone = JSON.parse(JSON.stringify(a));
console.log(typeof clone.date);  // result of .toISOString()

Reliable cloning using a library

Since cloning objects is not trivial (complex types, circular references, function etc.), most major libraries provide function to clone objects. Don’t reinvent the wheel - if you’re already using a library, check if it has an object cloning function. For example,


For completeness, note that ES6 offers two shallow copy mechanisms: Object.assign() and the spread operator.

Refer: https://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-deep-clone-an-object-in-javascript