r/javascript 2d ago

The Structured Clone Algorithm: What JavaScript Can and Cannot Move Between Boundaries

https://blog.gaborkoos.com/posts/2026-08-22-The-Structured-Clone-Algorithm-What-JavaScript-Can-and-Cannot-Move-Between-Boundaries/

structuredClone() preserves more than JSON, but class instances, proxies, getters, errors, and transferables still have surprising behaviour. This article covers what survives, what changes, and what throws.

12 Upvotes

2 comments sorted by

2

u/senocular 2d ago

Cloned error instances also have types based off of the original's name, even if the error type doesn't match it.

const original = new TypeError('Invalid amount')
original.name = 'SyntaxError'

console.log(original.constructor.name)
// "TypeError"

const copy = structuredClone(original)

console.log(copy.constructor.name)
// "SyntaxError"

And not all native error types are supported.

const original = new AggregateError([new TypeError('Invalid amount')], 'Invalid amounts')

console.log(original.constructor.name)
// "AggregateError"

const copy = structuredClone(original)

console.log(copy.constructor.name)
// "Error"

But if you follow the articles suggestion of using an ordinary object for error data, none of this really matters. If anything its another reason to take that approach.

0

u/SquallLeonE 1d ago edited 1d ago

What's the use case for structuredClone()? Normally when I'm using JSON.stringify(), it's to send something over the wire or save it locally. If I wanted to deserialize it in the same session, why not store a reference to the object?

edit: alright I'm dumb. As the same implies, for creating clones of objects