Copying a JavaScript object is easy to get wrong. A shallow copy such as { ...original } copies the top level but shares nested objects, so a change in the copy mutates the original. The common workaround, JSON.parse(JSON.stringify(original)), silently drops functions, turns Date values into strings, and throws on circular references.
structuredClone is a built-in function in browsers and Node.js that creates a deep copy, meaning every nested object and array is copied too, so the clone shares nothing with the original. It uses the structured clone algorithm, the same mechanism browsers use to pass data between threads with postMessage and to store values in IndexedDB. It handles circular references and built-in types such as Map, Set, and Date, and it throws a DataCloneError when it meets a value it cannot copy, such as a function or a DOM node. Use it when you are copying data. Reach for a library when you need to preserve class instances, methods, or custom cloning behavior.
What structuredClone copies and what throws DataCloneError
The structured clone algorithm supports the data types that show up in real payloads: primitives except symbol, plain objects, arrays, Map, Set, Date, RegExp, ArrayBuffer, TypedArray, DataView, and the standard Error types. For error objects, the name must be one of Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError, or URIError. If it is not, the clone sets name to "Error". Browsers serialize name and message, and they are expected to serialize other useful properties like stack and cause. One small detail: a cloned RegExp does not preserve lastIndex.
The function throws a DataCloneError DOMException if any part of the input is not serializable, as defined in the HTML standard’s structured data section. The values that trigger this are the ones the algorithm cannot duplicate: function objects, DOM nodes, Symbol values, WeakMap, and WeakSet. The algorithm cannot duplicate a function because it has no way to copy executable code and closure state. A DOM node is tied to the document it lives in, so it cannot be duplicated.
structuredClone keeps the shape of the graph, including cycles. If an object references itself, the clone references itself in the same way:
const original = { name: "config" };
original.self = original;
const clone = structuredClone(original);
console.log(clone.self === clone); // true
console.log(clone === original); // false
The clone is a new object with the same values. The algorithm also preserves shared references: when the same object appears in two places in the original, both places in the clone point to the same cloned object.
Class instances lose their prototype and methods
structuredClone does not walk the prototype chain. When you clone an instance of a class, you get a plain object with the same enumerable own properties, but the prototype is gone. Methods live on the prototype, so they disappear, and instanceof returns false. Private fields are not duplicated either.
class Counter {
constructor() {
this.count = 0;
}
increment() {
this.count += 1;
}
}
const counter = new Counter();
const clone = structuredClone(counter);
console.log(clone instanceof Counter); // false
console.log(typeof clone.increment); // undefined
The same limitation applies to metadata on plain objects. Property descriptors, getters, setters, and other accessor behavior are not duplicated; the clone contains the current values as plain data. If your code relies on instanceof, calls methods on the cloned value, or depends on accessors, structuredClone is the wrong tool. The call succeeds and returns an object that looks right, so the missing methods only show up when a method call fails.
Why the JSON trick fails where structuredClone works
JSON.parse(JSON.stringify(value)) is the older deep-copy workaround, and it fails in ways that are easy to miss. It throws on recursive data structures, it silently discards functions, and it corrupts built-in types: a Date becomes an ISO string, a Map or Set becomes {}, and a RegExp becomes {}. The silent parts are dangerous: the copy succeeds but contains less than the original.
const data = new Map([["tags", new Set(["a", "b"])]]);
console.log(JSON.parse(JSON.stringify(data))); // {}
console.log(structuredClone(data).get("tags")); // Set(2) { "a", "b" }
structuredClone handles all of those cases, which is why the web.dev guide to deep-copying describes it as generally more robust and often faster than the JSON hack. The JSON approach still has one legitimate niche: it produces a string, which you need when sending data over the wire, logging it, or storing it in a text-only store. For in-memory copying, structuredClone covers the data types that JSON cannot represent.
JSON.stringify calls a toJSON method on an object if one exists, which lets a class control its serialized form. structuredClone ignores toJSON and copies the object’s own properties directly.
Transferring ArrayBuffers instead of copying them
The second argument to structuredClone is an options object with a transfer array. Any transferable object listed there is moved into the clone instead of copied. For a large ArrayBuffer, this avoids duplicating the memory, which is the main reason to use it.
Transferring is irreversible. The original object is detached, meaning it is cleared and unusable. After transferring a Uint8Array‘s buffer, the original buffer’s byteLength becomes 0, as shown in the MDN documentation for structuredClone, and any attempt to use it throws a TypeError. The HTML spec says transferring is non-idempotent: once an object has been transferred, it cannot be transferred again or used at all.
const buffer = new ArrayBuffer(16);
const view = new Uint8Array(buffer);
view[0] = 42;
const transferred = structuredClone(buffer, { transfer: [buffer] });
console.log(buffer.byteLength); // 0
ArrayBuffer is the transferable you will use most often. MessagePort, ImageBitmap, and a few other platform objects are also transferable. Use the transfer list only when you are done with the original and want to move large memory without a copy.
When to reach for a cloning library
structuredClone has been widely available in browsers since March 2022, and it has been available in Node.js, so most code can rely on it directly. The cases that justify a library are the ones where its limitations are deal-breakers. Libraries like Lodash still provide custom implementations for exactly those situations, most notably cloning class instances while keeping their prototypes and methods.
A library also helps when you need to support engines older than the ones above. A polyfill of structuredClone exists in core-js, but it has a reported bug with transferring buffers, so verify the behavior before trusting it with data you cannot afford to corrupt.
Newer built-in types are a separate edge case. The structured clone algorithm is updated over time, and engines do not always agree on the newest types. For example, structured cloning of Temporal instances returns an empty object in Chrome while Firefox throws an error. If you copy values of a recently added built-in type, test the behavior in the engines you support.
structuredClone decision checklist
Run through these rules when you pick a copying strategy.
- Use
structuredClonewhen the value is data only: plain objects, arrays, primitives,Map,Set,Date,RegExp,ArrayBuffer, or typed arrays. - Use it when the graph has circular references or the same object appears in multiple places.
- Use it when you want an error instead of silent data loss, since a function or DOM node makes it throw.
- Avoid it when the value contains functions, DOM nodes,
Symbolvalues,WeakMap, orWeakSet. - Avoid it when cloning a class instance whose methods or
instanceofchecks matter. - Avoid it when you need getters, setters, property descriptors, or private fields.
- Use the
transferoption only for largeArrayBufferobjects you are ready to give up. - Use a library such as Lodash’s
cloneDeepfor prototype-preserving clones, custom cloning semantics, or engines withoutstructuredClone.