Peter Mekhaeil

Deep clone object with structuredClone()

structuredClone() is a native API in JavaScript that can do deep cloning of objects:


const original = {
  name: "Peter",
  properties: {
    age: new Date()
  }
};

const copy = structuredClone(original);

copy.properties.action = "Jump";
original.properties.action; // undefined

It can handle circular references and other JS built-in types such as Date, Set, Map.

Learn more on MDN.

What about object spread?

The object spread operator actually does a shallow copy. If you modify a deeply nested property, both objects are affected:

const original = {
  name: "Peter",
  properties: {
    age: new Date()
  }
};

const copy = { ...original };

copy.properties.action = "Jump";
original.properties.action; // "Jump"