# JavaScript Deep Clone Objects with structuredClone

> Deep clone JavaScript objects using the built-in structuredClone function, which handles nested objects and dates without the limitations of JSON serialization

**URL:** https://sentry.io/answers/what-is-the-most-efficient-way-to-deep-clone-an-object-in-javascript/

---

## The Problem

What is the most efficient way to deep clone an object in JavaScript?

## The Solution

Several methods for deep cloning objects in JavaScript have been invented over the years, with varying efficiency and effectiveness. Modern browsers provide the [`structuredClone()`](https://developer.mozilla.org/en-US/docs/Web/API/structuredClone) function, which will create a deep clone of an object provided to it using the [structured clone algorithm](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm). A code example:

```javascript
const person = {
  firstName: "John",
  lastName: "Doe",
  age: 35,
  purchaseHistory: [
    {
      productName: "apple",
      quantity: 5,
      datePurchased: new Date()
    },
    {
      productName: "banana",
      quantity: 3,
      datePurchased: new Date()
    },
    {
      productName: "pear",
      quantity: 4,
      datePurchased: new Date()
    }
  ]
};

const clone = structuredClone(person);

console.assert(clone !== person); // two distinct objects
console.assert(clone.firstName === "John"); // same values
```

Note that `structuredClone()` does not support object properties that are functions or DOM nodes, and will throw a `DataCloneError` if these are found in the object provided for cloning.

---

*Source: [sentry.io/answers/what-is-the-most-efficient-way-to-deep-clone-an-object-in-javascript/](https://sentry.io/answers/what-is-the-most-efficient-way-to-deep-clone-an-object-in-javascript/)*
