Sub topic 2
Which Types structuredClone Can Copy
This chapter assumes you know JavaScript objects, Map, Set and Date, and try/catch. It also assumes you have already called structuredClone once. You can run every example in Node.js 17+ or a modern browser console.
Supported Types
The structured clone algorithm copies a fixed list of types. It recreates each one as the same type, so it doesn't flatten them into strings or plain objects.
| Category | Examples |
|---|---|
| Primitives | numbers, strings, booleans, `null`, `undefined`, BigInt (but not Symbol) |
| Containers | plain objects, arrays, `Map`, `Set` |
| Built-ins | `Date`, `RegExp`, `Error` types, boxed primitives |
| Binary data | `ArrayBuffer`, typed arrays, `DataView` |
| Web platform | `Blob`, `File`, `ImageData` (browsers) |
The contents of containers are copied deeply as well:
const original = {
when: new Date(0),
ids: new Set([1, 2]),
meta: new Map([['k', { v: 1 }]])
};
const copy = structuredClone(original);
console.log(copy.when instanceof Date); // true
console.log(copy.ids.has(2)); // true
console.log(copy.meta.get('k') === original.meta.get('k')); // false
copy.meta.get('k').v = 99;
console.log(original.meta.get('k').v); // 1Circular and Shared References
The algorithm keeps a memory map from each original object to its copy. When it reaches an object it has already copied, it reuses that copy and does not make a second one. As a result, a cycle in the original becomes a cycle in the copy, and two references to one shared object become two references to one new object.
const node = { name: 'root' };
node.self = node;
const shared = { id: 1 };
const pair = { a: shared, b: shared };
const nodeCopy = structuredClone(node);
const pairCopy = structuredClone(pair);
console.log(nodeCopy.self === nodeCopy); // true
console.log(nodeCopy === node); // false
console.log(pairCopy.a === pairCopy.b); // true
console.log(pairCopy.a === shared); // falseA JSON round-trip handles these cases worse. It throws a TypeError on a cycle, and it splits a shared object into separate copies.
Functions, DOM Nodes and DataCloneError
Some values can't be cloned: functions, DOM nodes, Symbols, and objects such as WeakMap or Promise. structuredClone throws a DOMException named DataCloneError when it meets one of them. It never silently drops the value.
try {
structuredClone({ onClick: () => {} });
} catch (err) {
console.log(err.name); // 'DataCloneError'
}Exercise
What does structuredClone({ list: [1, 2], again: null }) return if you first set obj.again = obj.list? Is copy.again === copy.list?
Show answer
It returns a new object whose list and again point to the same new array, so copy.again === copy.list is true. Neither property is the original array.
Exercise
Which of these throw a DataCloneError: new Map([[1, 'a']]), { id: Symbol('x') }, new Uint8Array(4), { run() {} }?
Show answer
{ id: Symbol('x') } and { run() {} } throw, because Symbols and functions can't be cloned. The Map and the typed array are copied normally.