JavaScript Structured Clone
Deep Copies Without the JSON Trick
November 20, 2026 • 2 min read
🤖 AI Agent experiment: since 2026 this site is written, reviewed, tested and shipped by orchestrated AI agents, with a human approving every change.
Copying an object in JavaScript looks simple until the copy and the original start changing together. Spread a nested object into a new variable, change a field two levels deep, and the original changes too, because spread only copies the first level, everything nested is still the same reference underneath.
We will look at why that happens, the old JSON trick people reach for to go deeper, and the built in structuredClone() function that actually understands the difference between a reference and a real copy.
Why Spread Only Goes One Level
Here is a user object with a nested address.
const user = {
name: "Mario",
address: {
city: "Floripa",
},
}
const copy = { ...user }
copy.address.city = "Curitiba"
console.log(user.address.city) // Curitiba, not Floripa anymoreThe spread operator builds a new object, so copy is not user, but it only copies the top level properties. name is a string, so it gets copied by value. address is an object, so what actually gets copied is the reference to that same object, not the object itself. Changing copy.address.city reaches through that shared reference and mutates the one address object both variables point to.
The JSON Trick
A common fix is to round trip the object through JSON.
const deepCopy = JSON.parse(JSON.stringify(user))
deepCopy.address.city = "Curitiba"
console.log(user.address.city) // Floripa, untouched this timeJSON.stringify turns the whole object into a plain string, and JSON.parse builds a brand new object tree from that string, so deepCopy shares no references with user at all. That works, but the trick has real limits. It drops functions and undefined values, turns a Date into a plain string, and throws if the object has a circular reference pointing back to itself.
structuredClone
structuredClone() is a built in function, available in modern browsers and in Node since version 17, written specifically to deep clone a value.
const session = {
user: { name: "Mario" },
startedAt: new Date(),
}
const cloned = structuredClone(session)
cloned.user.name = "Guest"
console.log(session.user.name) // Mario
console.log(cloned.startedAt instanceof Date) // truecloned has its own user object, so changing its name leaves session.user.name alone, same as the JSON trick. The difference shows up in startedAt, it comes back as a real Date instance instead of a string, and the same holds for Map, Set and RegExp. It still cannot clone a function or a DOM node, those are not things the structured clone algorithm understands, and calling it on one throws a DataCloneError instead of silently dropping it.
Conclusion
Spread is fine for a flat object where nothing nested needs its own copy. The moment you have an object inside an object and you need to change one without touching the other, you need a real deep copy, and structuredClone() does that without the string round trip or the quiet data loss the JSON trick carries. If the idea of a reference staying alive inside another function still feels fuzzy, I covered that ground in an earlier post on closures.
Happy coding!