JavaScript

Deep-clone supported data with structuredClone

Use structuredClone to copy supported structured data, including nested objects and cyclic references. It is a better fit than JSON round-tripping when you need Dates, Maps, or other supported built-in types.

Worked example

Node.js example · save as example.mjs and run node example.mjs

const original = {created: new Date("2026-01-01T00:00:00Z"), tags: new Set(["a"])};
const copy = structuredClone(original);
copy.tags.add("b");
console.log(original.tags.size, copy.tags.size);
console.log(copy.created instanceof Date);

How it works

The nested set and date are cloned as their supported types, so modifying the copied set does not alter the original. JSON serialization would not retain the Set contents in the same form. Structured cloning also has defined handling for cycles.

Limits and mistakes

Functions and some platform objects cannot be cloned and may trigger DataCloneError. Custom class behavior and property descriptors are not a general-purpose serialization contract. Use an explicit schema when data crosses a persistent or network boundary.

Check the result

Expect 1 2 and then true. Include your actual data types in a small trial instead of assuming every object accepted by your application is cloneable.

Sources

Reference documentation for the behavior used in this guide: