Member-only story
structuredClone vs JSON.parse(JSON.stringify): The Boundary Decides What Survives
I copied one snapshot both ways in Node 20. Each lost something different, and the place the data goes picks the copy for you.
Santosh Yadav
9 min readJust now
Press enter or click to view image in full size
A snapshot goes into localStorage with a Date in it. It comes back out with a string in the same place. Nothing throws until a line much later calls savedAt.getTime() .
Most advice on deep copy in JavaScript stops at “use structuredClone instead of JSON.parse(JSON.stringify(x)) ". I think that advice answers the wrong question. You rarely get to pick the copy. The place your data is going picks it for you, and each of the two copies breaks different things.
So this post does two things. First, it puts both copies through the same snapshot and shows what each one keeps, drops and throws on.…