Shallow copy is also identified by pass by reference in other programming languages.
What is a Shallow copy?
When you make a copy of variable(original source) into another variable(new source) by using assignment operator(=) on that time only the reference address(memory address) of the original variable is copied by into another variable and not the actual value(objects, array, etc). At last, both variables refer to the same value. So when you make any changes in the new variable it will also mutate(Change) the original variable.
Deep copy is also identified by pass by value in other programming languages.
What is a Deep copy?
When you make a copy of variable(original source) into another variable(new source) by using assignment operator(=) on that time all the values(objects, array, etc) along with the reference address(memory address) of the original variable is copied by into another variable. At last, both variables refer to the different referenced values. So when you make any changes in the new variable it won’t mutate the original variable.
Now, Let’s try to digest this concept with a few examples.
Now let’s take the example for non-primitive data types(Object, Array, Set, Functions, etc.).
1. Use assignment(=) operator:
This is the basic operator we use in our day to day life for achieving shallow copy.
This is a straightforward example of doing a shallow copy. As you find in the above image that when I make a change in copiedVal(New variable)then it also mutants the originalVal(Original source).
2. Use JSON.stringify() method:
JSON.stringify() method quite enough able to provide deep copy. However, there is one disadvantage of using this method is that when you use JSON.stringify on an object which has a function as value then it won’t able to justify the copy operation. As you can see in the above example it has brought out key getAge from copiedVal as it’s type is Function. I would recommend use this method only if your objects have not any key with Function type.
Note: This method provides full deep copy but doesn’t work with those data which have the value of Function type.
3. Use Object.assign():
Note: Object.assign() won’t work effectively when you have a nested data structure.
4. Use spread(…) operator:
The spread operator does the same thing as Object.assign() method does. It is also able to do a partial deep copy.
Note: Spread operator won’t work effectively when you have a nested data structure.
5. Use _.CloneDeep():
- _.cloneDeep() method is befitting for 100% full deep copy in case if you want to achieve it.
2. _.clone() method can be used to achieve a shallow copy.
6. Use your own Custom function:
You can also write your own function based on your requirements to achieve a shallow or deep copy. One key fact using this approach is handling all the edge cases so it won’t break your code. But I’m always skeptical about this approach.
7. Use slice() method:
When you have a data type of Array type and you wan to do a deep copy then the slice() method is a good alternative to do that.
Note: slice() method by itself is not enough to clone a multidimensional array completely. It is also not able to provide a full deep copy.