Sub topic 5
Which Types structuredClone Can Copy
This chapter assumes you know JavaScript objects, arrays and primitives, and the basics of Map, Set and Date. It also builds on the previous chapter, where you made your first deep copy with structuredClone. Here you will learn which kinds of values the structured clone algorithm accepts, and how each kind looks after the copy.
The working model
The structured clone algorithm is the set of rules structuredClone follows. It walks your value and checks the type of every piece. When it knows a type, it builds a new value of that same type and copies the contents. When it does not know a type, it throws an error. A later chapter covers those errors.
The supported types fall into a few groups.
The supported types at a glance
| Type | What the copy looks like | Things to watch for |
|---|---|---|
| Primitives except symbol | The same value | `NaN`, `-0` and `undefined` are kept, unlike with JSON |
| Plain object, Array | New object or array with copied own enumerable properties | Array holes stay holes |
| Date | New `Date` with the same time value | Stays a real `Date`, not a string |
| RegExp | New `RegExp` with the same source and flags | `lastIndex` is reset to 0 |
| Map, Set | New `Map` or `Set`; keys and values are deep copied | Object keys become new objects |
| Error and its standard subtypes | New error with `name` and `message` | Custom properties and subclasses may be lost |
| Boolean, String, Number wrapper objects | New wrapper object | Rarely used, but supported |
| ArrayBuffer, typed arrays, DataView | New buffer holding a copy of the bytes | The whole underlying buffer is copied |
| Blob, File, ImageData and similar | New platform object | These depend on the runtime, mostly browsers |
Symbols are the one primitive that is not supported. Passing a symbol as a value throws.
Seeing the types survive
Run this in Node.js 17 or later, or in a modern browser console.
const original = {
count: 10n,
ratio: NaN,
nothing: undefined,
when: new Date(0),
pattern: /ab+c/gi,
lookup: new Map([[{ id: 1 }, 'first']]),
seen: new Set(['x', 'y']),
failure: new RangeError('too big'),
bytes: new Uint8Array([1, 2, 3])
};
const copy = structuredClone(original);
console.log(typeof copy.count); // 'bigint'
console.log(Number.isNaN(copy.ratio)); // true
console.log('nothing' in copy); // true
console.log(copy.when instanceof Date); // true
console.log(copy.pattern.flags); // 'gi'
console.log([...copy.lookup.keys()][0].id); // 1
console.log(copy.seen.has('y')); // true
console.log(copy.failure instanceof RangeError, copy.failure.message); // true 'too big'
console.log(copy.bytes instanceof Uint8Array); // true
copy.bytes[0] = 99;
console.log(original.bytes[0]); // 1Every value keeps its type, and changing the copied bytes does not change the original.
Common errors
Assuming Map keys keep their identity
When a Map key is an object, the copy gets a new key object. So copy.lookup.get(someOriginalKey) returns undefined. You must look up keys through the copied map, as the example does with keys().
Expecting RegExp state to carry over
A global or sticky regular expression tracks its position in lastIndex. The clone starts again at 0. If your code depends on where a previous exec call stopped, store that position somewhere else.
Copying a small view of a large buffer
A typed array is a view onto an ArrayBuffer. The algorithm copies the entire underlying buffer, not only the part the view covers.
const big = new ArrayBuffer(1024 * 1024);
const view = new Uint8Array(big, 0, 4);
const copied = structuredClone(view);
console.log(copied.length); // 4
console.log(copied.buffer.byteLength); // 1048576To copy only the bytes you need, call view.slice() first. It returns a typed array backed by a new, right-sized buffer.
Relying on custom Error details
Standard error types such as TypeError and RangeError keep their type and message. An instance of your own class ValidationError extends Error usually comes back as a plain Error, and extra properties such as code may be missing. Check this before you send errors between threads.
Exercise
You clone { tags: new Set([{ id: 7 }]), re: /a/g } after calling re.exec('aaa') on the original. What is copy.re.lastIndex, and is the object inside the copied Set the same object as the one in the original Set?
Show answer
copy.re.lastIndex is 0, because cloning a RegExp copies only its source and flags. The object inside the copied Set is a new object with id: 7, so comparing it to the original with === gives false.
Exercise
A Float32Array of 10 elements views a 4 MB ArrayBuffer. How many bytes does structuredClone copy, and how can you reduce that?
Show answer
It copies the full 4 MB buffer. Call .slice() on the typed array first, which produces a 40-byte buffer (10 × 4 bytes), and clone that.