Shallow Copy and Deep Copy in JavaScript

JavaScript provides two methods for duplicating objects: Shallow copying and Deep copying. Recognizing the variances between these duplication approaches is crucial in various programming situations due to their distinct advantages and limitations.

Shallow Copy

A shallow copy duplicates an object only at the surface level, copying the object and its immediate properties. However, any nested objects or arrays within the copied object will remain linked to the original object's memory location. This implies that modifications made to the nested object will impact both the original and copied objects simultaneously.

An illustration showcasing the process of performing a shallow copy of an object by utilizing the spread operator is demonstrated below:

Example

const originalObject = { a: 1, b: { c: 2 } };

const shallowCopy = { ...originalObject };

In this instance, the originalObject contains a nested object 'b' with a property 'c'. By employing the spread operator ..., a new object named shallowCopy is generated to replicate originalObject in a shallow manner. Consequently, modifications made to shallowCopy will not impact originalObject:

Example

shallowCopy.a = 3; // Changes shallowCopy, but not originalObject

shallowCopy.b.c = 4; // Changes both shallowCopy and originalObject

Modifying the value of the property 'a' in shallowCopy does not impact originalObject. Nevertheless, altering the value of the property 'c' in the nested object 'b' impacts both shallowCopy and originalObject.

This behavior occurs because both shallowCopy and originalObject hold a reference to the identical nested object b. Any modifications made to b in shallowCopy will directly impact the corresponding object in originalObject.

Deep Copy

A deep copy refers to creating a duplicate object with distinct memory addresses for all its attributes and nested elements, like objects or arrays. This ensures that modifications made to the replicated object or any of its nested elements do not impact the original object.

An illustration of performing a deep copy of an object involves utilizing the functions JSON.parse and JSON.stringify.

Example

const originalObject = { a: 1, b: { c: 2 } };

const deepCopy = JSON.parse(JSON.stringify(originalObject));

In this instance, we utilize JSON.stringify to transform the originalObject into a JSON string, and subsequently employ JSON.parse to change that string into a fresh object named deepCopy. This process results in the creation of a deep duplicate of the originalObject.

Modifying the deepCopy function will no longer impact the originalObject.

Example

deepCopy.a = 3; // Changes deepCopy, but not originalObject

deepCopy.b.c = 4; // Changes deepCopy, but not originalObject

Modifying the property a or the nested object b in deepCopy does not impact originalObject since they have distinct memory locations for all their properties and nested objects. This isolation ensures that changes made in one object do not influence the other.

Performance Considerations

It is worth mentioning that deep copying can be more resource-intensive compared to shallow copying. This is due to the fact that generating a new object and duplicating all its attributes and inner objects may require more computational resources and memory allocation than just creating a new object with pointers to the identical memory addresses as the initial object.

There are situations where shallow copying can offer better efficiency and convenience. Yet, when it is crucial to ensure that modifications made to a duplicated object do not impact the original object, deep copying becomes essential.

Conclusion

JavaScript provides two methods for duplicating objects: shallow copy and deep copy. When performing a shallow copy, a new object is generated with pointers to the identical memory addresses as the original object. In contrast, deep copying involves creating a fresh object with unique memory addresses for each of its attributes and embedded objects or arrays.

Performing a shallow copy can offer better performance, but it can lead to unexpected outcomes if modifications made to the copied object impact the original one. On the other hand, deep copying guarantees that alterations to a copied object do not influence the original, albeit at a potentially higher performance cost.

When it comes to making a duplicate of an object, JSON.parse and JSON.stringify provide a straightforward approach, but it may not be universally applicable. For scenarios where an object with functions or circular references needs to be duplicated, resorting to a recursive deep copy function becomes essential.

Input Required

This code uses input(). Please provide values below: