Search code examples
javascriptjavascript-objectsshallow-copyspread-syntax

Object copy using spread syntax actually shallow or deep?


I understand spread syntax makes a shallow copy of objects, i.e., the cloned object refers to the same reference as the original object.

However, the actual behaviour seems contradicting and confusing.

const oldObj = {a: {b: 10}};

const newObj = {...oldObj};

oldObj.a.b = 2;
newObj  //{a: {b: 2}}
oldObj  //{a: {b: 2}}

Above behaviour makes sense, newObj is also updated by updating oldObj because they refer the same location.

const oldWeirdObj = {a:5,b:3};

const newWeirdObj = {...oldWeirdObj};

oldWeirdObj.a=2;
oldWeirdObj   //{a:2,b:3}
newWeirdObj   //{a:5,b:3}

I don't understand, why is newWeirdObj not updating similar to oldWeirdObj?

They are still referring to the same location if I am not wrong, but why is the update to oldWeirdObj not updating newWeirdObj?


Solution

  • So, for this problem, you have to understand what is the shallow copy and deep copy.

    Shallow copy is a bit-wise copy of an object which makes a new object by copying the memory address of the original object. That is, it makes a new object by which memory addresses are the same as the original object.

    Deep copy, copies all the fields with dynamically allocated memory. That is, every value of the copied object gets a new memory address rather than the original object.

    Now, what a spread operator does? It deep copies the data if it is not nested. For nested data, it deeply copies the topmost data and shallow copies of the nested data.

    In your example,

    const oldObj = {a: {b: 10}};
    const newObj = {...oldObj};
    

    It deep copy the top level data, i.e. it gives the property a, a new memory address, but it shallow copy the nested object i.e. {b: 10} which is still now referring to the original oldObj's memory location.

    If you don't believe me check the example,

    const oldObj = {a: {b: 10}, c: 2};
    const newObj = {...oldObj};
    
    oldObj.a.b = 2; // It also changes the newObj `b` value as `newObj` and `oldObj`'s `b` property allocates the same memory address.
    oldObj.c = 5; // It changes the oldObj `c` but untouched at the newObj
    
    
    
    console.log('oldObj:', oldObj);
    console.log('newObj:', newObj);
    .as-console-wrapper {min-height: 100%!important; top: 0;}

    You see the c property at the newObj is untouched.

    How do I deep copy an object.

    There are several ways I think. A common and popular way is to use JSON.stringify() and JSON.parse().

    const oldObj = {a: {b: 10}, c: 2};
    const newObj = JSON.parse(JSON.stringify(oldObj));
    
    oldObj.a.b = 3;
    oldObj.c = 4;
    
    console.log('oldObj', oldObj);
    console.log('newObj', newObj);
    .as-console-wrapper {min-height: 100%!important; top: 0;}

    Now, the newObj has completely new memory address and any changes on oldObj don't affect the newObj.

    Another approach is to assign the oldObj properties one by one into newObj's newly assigned properties.

    const oldObj = {a: {b: 10}, c: 2};
    const newObj = {a: {b: oldObj.a.b}, c: oldObj.c};
    
    oldObj.a.b = 3;
    oldObj.c = 4;
    
    console.log('oldObj:', oldObj);
    console.log('newObj:', newObj);
    .as-console-wrapper {min-height: 100%!important; top: 0;} 

    There are some libraries available for deep-copy. You can use them too.