Understanding Shallow Copy vs Deep Copy in Programming

Code Interview: Understanding Shallow Copy vs Deep Copy in Programming

Shallow Copy

  • A shallow copy creates a new object, but inserts references into it to the objects found in the original.
  • Changes made to a shallow copy of an object can reflect in the original object if the changes are made to a mutable object contained within the shallow copy.

Example:

import copy

original_list = [1, 2, [3, 4]]
shallow_copy = copy.copy(original_list)

shallow_copy[2][0] = 'changed'
print("Original List:", original_list)  # [1, 2, ['changed', 4]]
print("Shallow Copy:", shallow_copy)    # [1, 2, ['changed', 4]]

Deep Copy

  • A deep copy creates a new object and recursively adds the copies of nested objects present in the original.
  • Changes made to a deep copy of an object do not affect the original object.

Example:

import copy

original_list = [1, 2, [3, 4]]
deep_copy = copy.deepcopy(original_list)

deep_copy[2][0] = 'changed'
print("Original List:", original_list)  # [1, 2, [3, 4]]
print("Deep Copy:", deep_copy)          # [1, 2, ['changed', 4]]

Shallow Copy vs Deep Copy in JavaScript

Shallow Copy

  • A shallow copy copies an object’s properties into a new object. If the property value is a reference to an object, only the reference is copied.

Example:

let originalArray = [1, 2, [3, 4]];
let shallowCopy = originalArray.slice();

shallowCopy[2][0] = 'changed';
console.log("Original Array:", originalArray); // [1, 2, ['changed', 4]]
console.log("Shallow Copy:", shallowCopy);     // [1, 2, ['changed', 4]]

Deep Copy

  • A deep copy creates a new object and recursively copies all properties, ensuring that no references to the original objects are retained.

Example:

let originalArray = [1, 2, [3, 4]];
let deepCopy = JSON.parse(JSON.stringify(originalArray));

deepCopy[2][0] = 'changed';
console.log("Original Array:", originalArray); // [1, 2, [3, 4]]
console.log("Deep Copy:", deepCopy);           // [1, 2, ['changed', 4]]

Key Points

  • Shallow copies are faster but do not handle nested objects.
  • Deep copies are slower but ensure complete independence from the original object.

Would you like a visual representation of these concepts?

Further Reading:


What is the difference between deep copy and shallow copy in JavaScript ? Interview Happy

Comments

One response to “Code Interview: Understanding Shallow Copy vs Deep Copy in Programming”

  1. admin Avatar

    ### 浅复制 vs 深复制 在 Python 中

    #### 浅复制
    – 浅复制创建一个新对象,但插入对原始对象中找到的对象的引用。
    – 如果对浅复制的对象中包含的可变对象进行更改,这些更改可能会反映在原始对象中。

    **示例:**

    ```python
    import copy

    original_list = [1, 2, [3, 4]]
    shallow_copy = copy.copy(original_list)

    shallow_copy[2][0] = 'changed'
    print("原始列表:", original_list) # [1, 2, ['changed', 4]]
    print("浅复制:", shallow_copy) # [1, 2, ['changed', 4]]
    ```

    #### 深复制
    – 深复制创建一个新对象,并递归地添加原始对象中存在的嵌套对象的副本。
    – 对深复制的对象所做的更改不会影响原始对象。

    **示例:**

    “`python
    import copy

    original_list = [1, 2, [3, 4]]
    deep_copy = copy.deepcopy(original_list)

    deep_copy[2][0] = ‘changed’
    print(“原始列表:”, original_list) # [1, 2, [3, 4]]
    print(“深复制:”, deep_copy) # [1, 2, [‘changed’, 4]]
    “`

    ### 浅复制 vs 深复制 在 JavaScript 中

    #### 浅复制
    – 浅复制将对象的属性复制到一个新对象中。如果属性值是对对象的引用,则仅复制引用。

    **示例:**

    “`javascript
    let originalArray = [1, 2, [3, 4]];
    let shallowCopy = originalArray.slice();

    shallowCopy[2][0] = ‘changed’;
    console.log(“原始数组:”, originalArray); // [1, 2, [‘changed’, 4]]
    console.log(“浅复制:”, shallowCopy); // [1, 2, [‘changed’, 4]]
    “`

    #### 深复制
    – 深复制创建一个新对象,并递归地复制所有属性,确保不保留对原始对象的引用。

    **示例:**

    “`javascript
    let originalArray = [1, 2, [3, 4]];
    let deepCopy = JSON.parse(JSON.stringify(originalArray));

    deepCopy[2][0] = ‘changed’;
    console.log(“原始数组:”, originalArray); // [1, 2, [3, 4]]
    console.log(“深复制:”, deepCopy); // [1, 2, [‘changed’, 4]]
    “`

    #### 关键点
    – 浅复制速度更快,但不能处理嵌套对象。
    – 深复制速度较慢,但确保与原始对象完全独立。

    您需要这些概念的可视化表示吗?

Leave a Reply

Your email address will not be published. Required fields are marked *