structuredClone Is the Built In Deep Copy for JavaScript
structuredClone makes a deep copy that keeps dates, maps, sets and cycles, and fails on functions. See what it drops, DataCloneError and the transfer option.

For years, the answer to the question of how to deep copy an object in JavaScript was JSON.parse(JSON.stringify(obj)), followed by an apology about dates. structuredClone() replaced it. It is a global function in Node.js 17 and later, in every current browser and in workers, and it runs the same algorithm the platform uses to pass messages between threads. It copies far more types than JSON, and it has a well-defined list of things it refuses. This post shows what it keeps, where it throws, the class instance surprise, and the transfer option that moves memory instead of copying it. Examples run on Node.js 24. The broader decision about copying is in the post on shallow and deep copies, and the model of references that makes copies necessary is in the post on primitives versus objects.
What it keeps
The algorithm handles the built-in data types that JSON cannot, and it preserves shared and circular references.
const shared = { id: 1 };
const source = {
when: new Date("2026-05-06T07:08:09Z"),
tags: new Set(["a", "b"]),
lookup: new Map([[{ k: 1 }, "value"]]),
pattern: /ab+c/gi,
bytes: new Uint8Array([1, 2, 3]),
big: 12345678901234567890n,
nested: { list: [1, [2, 3]], undef: undefined, nan: NaN },
first: shared,
second: shared,
};
source.self = source;
const clone = structuredClone(source);
console.log(clone.when instanceof Date, clone.when.getTime() === source.when.getTime());
console.log(clone.tags, clone.lookup.size);
console.log(clone.pattern.flags, clone.bytes, clone.big);
console.log(clone.nested);
console.log(clone.first === clone.second, clone.first === shared);
console.log(clone.self === clone);true true
Set(2) { 'a', 'b' } 1
gi Uint8Array(3) [ 1, 2, 3 ] 12345678901234567890n
{ list: [ 1, [ 2, 3 ] ], undef: undefined, nan: NaN }
true false
trueEvery type came back as the same type. undefined and NaN inside the data survived, unlike in JSON. The two properties that pointed at one shared object still point at one object, now a new one, and the self-reference points at the clone. Preserving shared structure is easy to overlook and matters: a naive recursive copy would duplicate the shared object and loop forever on the cycle.
What it refuses
Functions, symbols and a few host objects cannot be cloned, and the call throws a DataCloneError.
const attempts = {
function: { run() {} },
arrow: () => 1,
symbolValue: Symbol("id"),
weakMap: new WeakMap(),
promise: Promise.resolve(1),
};
for (const [label, value] of Object.entries(attempts)) {
try {
structuredClone(value);
console.log(label.padEnd(12), "cloned");
} catch (error) {
console.log(label.padEnd(12), error.name);
}
}
console.log(structuredClone({ ok: 1, [Symbol("skipped")]: 2 }));function DataCloneError
arrow DataCloneError
symbolValue DataCloneError
weakMap DataCloneError
promise DataCloneError
{ ok: 1 }A function, an arrow, a symbol value, a WeakMap and a promise all throw. A symbol used as a key is not an error: the property is silently dropped. The failure for functions is the one you will meet most, because a state object that carries a callback cannot be cloned, and the fix is to clone only the data and keep the behavior separate. The error message names the value that failed, which helps when the object is large.
Class instances become plain objects
The clone is a plain object with the same own enumerable data, and it does not remember the class.
class Money {
constructor(amount, currency) {
this.amount = amount;
this.currency = currency;
}
format() {
return `${this.amount} ${this.currency}`;
}
}
const price = new Money(10, "USD");
const copy = structuredClone(price);
console.log(copy);
console.log(copy instanceof Money, typeof copy.format);
console.log(Object.getPrototypeOf(copy) === Object.prototype);
const rebuilt = Object.assign(new Money(0, ""), copy);
console.log(rebuilt.format());{ amount: 10, currency: 'USD' }
false undefined
true
10 USDThe data came across and the methods did not. If a structure contains class instances, either rebuild them after the clone, as rebuilt does, or give the class its own clone() method. Private fields declared with # are not copied either, since they are not properties. This is the main reason structuredClone() is a tool for plain data and not for domain objects with behavior.
Getters, descriptors and non-enumerable properties
The algorithm reads own enumerable properties by value and writes plain data properties.
const source = {
visible: 1,
get computed() {
return "from getter";
},
};
Object.defineProperty(source, "hidden", { value: "secret", enumerable: false });
Object.freeze(source);
const clone = structuredClone(source);
console.log(Object.getOwnPropertyDescriptor(clone, "computed"));
console.log("hidden" in clone);
console.log(Object.isFrozen(clone));
console.log(Object.isFrozen(source));{
value: 'from getter',
writable: true,
enumerable: true,
configurable: true
}
false
false
trueThe getter was evaluated, and the result is a plain writable property on the clone. The hidden property was not copied. And the freeze did not carry over: the clone is a normal mutable object. If you rely on flags and accessors, copy with Object.getOwnPropertyDescriptors, as in the post on property descriptors.
Errors and other special objects
Built-in error types are cloned, with their message and stack in current engines, though custom properties on errors are not guaranteed.
const error = new TypeError("bad input", { cause: "upstream" });
error.code = "E_INPUT";
const clone = structuredClone(error);
console.log(clone instanceof TypeError);
console.log(clone.message, clone.cause);
console.log(clone.code);
console.log(typeof clone.stack);true
bad input upstream
undefined
stringThe error kept its type, message and cause. The custom code property depends on the engine, so do not rely on it. When an error needs to cross a worker boundary with extra fields, serialize it yourself into a plain object and rebuild it on the other side.
The transfer option
structuredClone() takes a second argument with a transfer list. For an ArrayBuffer, transferring moves the underlying memory to the clone and detaches the original, so no bytes are copied.
const original = new ArrayBuffer(8);
new Uint8Array(original).set([1, 2, 3]);
const copied = structuredClone(original);
console.log(original.byteLength, copied.byteLength);
const moved = structuredClone(original, { transfer: [original] });
console.log(original.byteLength, original.detached);
console.log(moved.byteLength, new Uint8Array(moved).slice(0, 3));
try {
new Uint8Array(original).length;
console.log("read ok");
} catch (error) {
console.log(error.name);
}8 8
0 true
8 Uint8Array(3) [ 1, 2, 3 ]
TypeErrorThe plain clone duplicated the eight bytes, so both buffers had length 8. The transfer left the original with zero bytes and detached set, and the new buffer holds the data. For a large buffer, such as an image or an audio chunk, transfer avoids a copy of the whole memory, and that is the reason the option exists. Workers use the same mechanism through postMessage(data, [buffer]), so the clone and the message behave the same way.
Using it for snapshots and undo
A common use is taking a snapshot of mutable state before a risky change, so the change can be rolled back. A clone is cheaper than reasoning about what might have been edited, and it is independent of later edits to the original.
const form = { name: "Ada", tags: ["a"], when: new Date(0) };
const history = [];
function edit(change) {
history.push(structuredClone(form));
change(form);
}
function undo() {
const previous = history.pop();
if (previous) Object.assign(form, previous);
}
edit((f) => f.tags.push("b"));
edit((f) => (f.name = "Grace"));
console.log(form.name, form.tags);
undo();
console.log(form.name, form.tags);
undo();
console.log(form.name, form.tags, form.when instanceof Date);Grace [ 'a', 'b' ]
Ada [ 'a', 'b' ]
Ada [ 'a' ] trueEach edit saved a full snapshot first, and each undo restored one. Because the clone is deep, the array inside the form was restored to its earlier contents and not left shared. The date stayed a date. For a long history of a large object, storing a full copy per step grows quickly, and storing patches or using structural sharing is the scalable design. For a form with a few dozen fields and a short history, a plain snapshot is the simplest thing that works.
Cost and when to measure
The clone walks the whole structure and allocates a new object for each node, so its cost is proportional to the size of the data. It is fast for small objects and noticeable for megabytes of nested data. If a profile shows time in structuredClone(), look at three options: clone less by narrowing the data to what you need, avoid the clone by using immutable updates, or transfer buffers instead of copying them. We would not guess at any of these without a profile, since the default is rarely the bottleneck.
Where it fits and where it does not
Use it for plain data: configuration, API responses, form state, undo snapshots and anything that came from JSON or the structured types above. It is faster to write and more faithful than a JSON round trip, and it handles cycles, which a hand-written recursive copy usually forgets.
Do not use it for objects with behavior, for anything that holds a function, or for DOM nodes. For those, write a clone() method or copy the data you need. And do not clone out of habit: a function that never mutates its input needs no copy, and a copy of a large structure costs time and memory in proportion to its size, so measure before calling it in a hot path.
Older runtimes may lack it. Node.js 16 and below do not have the global, and a polyfill built on a JSON round trip silently loses the types listed above, so check the support in your targets before depending on it. Test environments such as jsdom have had gaps in the past, so run your clone-dependent tests in the real runtime.
The algorithm is part of the HTML Standard, under structured cloning, and MDN documents the function and its supported types on its structuredClone reference page.


