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

In this tutorial, you'll be learning how to create a deep clone or also known as deep copy of a JavaScript object?
Submitted by Pratishtha Saxena, on May 16, 2022

Deep Clone

There are two types of copies – Shallow copy and Deep copy. In Shallow copy, only the address/reference of the original object is copied and hence the changes made to copied object are also reflected in the original object. Whereas in Deep copy the actual original object is copied and both the original and the copied objects are stored. So, even if we make any changes to the copied object, the original object remains unchanged.

Now, let's see how to make a deep clone of an object in JavaScript?

object2 = JSON.parse(JSON.stringify(object1))

By using this, first, we'll stringify object1 into a JSON string and then again create another object through parsing it.

There is an object person1 with some values in it. An object person2 has been created as a deep copy of person1.

let person1 = {name:'John', age:20, place:'Delhi'};
let person2 = JSON.parse(JSON.stringify(person1));

console.log(person1);
console.log(person2);

Output:

{name: 'John', age: 20, place: 'Delhi'}
{name: 'John', age: 20, place: 'Delhi'}

Now, let's check whether the created person2 is a deep copy of person1 or not. Let's say, we'll change the age in person2 and if it is a deep copy, it should not reflect the change in object1.

let person1 = {name:'John', age:20, place:'Delhi'};
let person2 = JSON.parse(JSON.stringify(person1));

person2.age=25; // changing the value in person2

console.log(person1);
console.log(person2);

Output:

{name: 'John', age: 20, place: 'Delhi'}
{name: 'John', age: 25, place: 'Delhi'}

As the value is only changed in person2, we can say that it is a deep copy/clone of a person1.

Hence, this is the most efficient way of creating a deep clone of any object in JavaScript.

JavaScript Examples »


ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.