JSON.parse(JSON.stringify()) VS structuredClone()
The right way (and the hacky way) to clone objects in JavaScript
If you’ve ever needed to clone an object in JavaScript, you’ve probably done this:
const clone = JSON.parse(JSON.stringify(obj));And felt that brief “hacker mode activated” moment.
But… you’ve probably also broken something without understanding why. Am I wrong?
Today we’re going to compare:
JSON.parse(JSON.stringify())structuredClone()
And understand when to use them, when to avoid them, and why you might be silently losing data without realizing it.
The Problem: Deep Cloning
In JavaScript, copying objects isn’t as simple as it looks.
const user = { name: "Ana" };
const copy = user;
copy.name = "Carlos";
console.log(user.name); // "Carlos"That happens because objects are copied by reference.
If we want a real copy, we need a deep clone.
The Classic Hack
const clone = JSON.parse(JSON.stringify(obj));It works… sometimes 😅
How does it work?
- Convert the object to a string
- Convert the string back into an object
Simple. Clever. Full of traps.
🔥 The Real Problem
Look at this example:
const obj = {
name: "Lucas",
age: 30,
birth: new Date(),
greet() { console.log("Hello") },
undef: undefined
};
const clone = JSON.parse(JSON.stringify(obj));
console.log(clone);Result?
{
name: "Lucas",
age: 30,
birth: "2026-02-19T00:00:00.000Z"
}💀 What disappeared?
- ❌ Functions
- ❌
undefined - ❌
Dateturned into a string - ❌ Map, Set, BigInt, RegExp… all broken
That’s the hidden cost of the “quick fix”.
structuredClone() — The Modern Way
Now look at this:
const clone = structuredClone(obj);That’s it.
Get Gutu Galuppo’s stories in your inbox
Join Medium for free to get updates from this writer.
No parse.
No stringify.
No trauma.
What Does It Support?
const obj = {
date: new Date(),
map: new Map([["a", 1]]),
set: new Set([1,2,3]),
nested: { value: 42 }
};
const clone = structuredClone(obj);
console.log(clone);Console result:
{
date: 2026-02-19T12:34:56.789Z,
map: Map(1) { 'a' => 1 },
set: Set(3) { 1, 2, 3 },
nested: { value: 42 }
}👉 What that means:
dateis still aDateobjectmapis still aMapsetis still aSetnestedstays intact
Nothing became a string.
Nothing disappeared.
Nothing was sacrificed on the altar of JSON.stringify() 😌
⚡ What It Does NOT Support
- ❌ Functions
- ❌ DOM Nodes
- ❌ Some complex custom class instances
And here’s the important part:
This is intentional.
structuredClone() is based on the Structured Clone Algorithm, which is used internally by the browser (like in postMessage, IndexedDB, and other platform APIs).
So it’s not a random utility — it’s built on core browser mechanics.
Direct Comparison
You can test this directly in your browser console:
const complexObject = {
name: "Test",
date: new Date(),
map: new Map([["key", "value"]]),
set: new Set([1, 2, 3]),
nested: {
arr: [1, 2, 3]
}
};
console.log("JSON clone:");
console.log(JSON.parse(JSON.stringify(complexObject)));
console.log("structuredClone:");
console.log(structuredClone(complexObject));Compare the results and see the difference yourself.
Performance: Is It Worth Switching?
Yes.
Besides being more correct, structuredClone():
- Is safer
- Is more expressive
- Prevents silent bugs
- Works with modern data types
It’s supported in:
- Chrome 98+
- Node 17+
- Modern Firefox
- Modern Edge
If you still need to support very old environments, then you might need a fallback. Otherwise… there’s really no reason not to use it.
When Should You Still Use JSON.parse(JSON.stringify())?
Honestly?
Almost never.
Maybe only if:
- You’re 100% sure the object is a simple POJO
- There’s no
Date,Map,Set,BigInt, orundefined - You’re stuck in an ancient environment
But nowadays, that’s increasingly rare.
Conclusion
JSON.parse(JSON.stringify()) was a clever workaround.
But structuredClone() is the correct solution.
It’s like comparing:
Electrical tape
vs
The proper tool
Does the tape work? Sure.
But do you really want your architecture depending on it?
If you enjoyed this kind of straight-to-the-point content with real examples, save this article and share it with that friend who’s still cloning objects with the JSON hack






