Course contentsShow
← JavaScript
Lesson 2398 of 2,72349. Common Patterns and IdiomsFree lesson

structuredClone() API

Using the modern structuredClone() method for reliable deep cloning with support for more types than JSON.

The structuredClone() API

You'll learn how to use the modern structuredClone() method for robust deep cloning that handles more data types than the JSON workaround.

What Is structuredClone()?

structuredClone() is a built-in JavaScript function that creates a true deep copy of complex objects. Unlike JSON.parse(JSON.stringify()), which you've already seen, structuredClone() can handle a much wider range of data types including Date, Map, Set, ArrayBuffer, RegExp, and even circular references.

Think of it like a professional photocopier that can replicate not just text, but also diagrams, colors, and embedded documents—while the JSON approach is like a basic text-only copier.

How It Works

const original = {
  date: new Date(),
  map: new Map([['key', 'value']]),
  set: new Set([1, 2, 3]),
  nested: { deep: { value: 42 } }
};

const clone = structuredClone(original);

clone.nested.deep.value = 100;
console.log(original.nested.deep.value); // 42 (unchanged)
console.log(clone.date instanceof Date); // true

What It Handles Better

// Circular references - JSON method would throw
const obj = { name: 'circular' };
obj.self = obj;
const copy = structuredClone(obj); // Works!

// Complex types preserved
const data = {
  buffer: new ArrayBuffer(8),
  regex: /test/gi
};
const cloned = structuredClone(data);

Limitations

structuredClone() cannot copy functions, DOM nodes, or prototypes. It creates plain objects from class instances.

Key Takeaway: Use structuredClone() for reliable deep cloning in modern JavaScript—it handles dates, collections, circular references, and binary data that the JSON method cannot.