Engineering

JavaScript Deep Cloning: structuredClone vs JSON.stringify

B

Boundev Team

Mar 21, 2026
12 min read
JavaScript Deep Cloning: structuredClone vs JSON.stringify

Stop breaking your JavaScript applications with shallow copies. Learn how structuredClone handles circular references, Maps, Sets, and complex data structures that JSON stringify cannot touch.

Key Takeaways

structuredClone() is the modern native solution that handles circular references, Maps, Sets, and complex types
JSON.stringify() breaks on functions, undefined, and circular references—it loses data silently
Spread operator and Object.assign create shallow copies only—nested objects remain shared references
Lodash cloneDeep adds 17KB+ to your bundle—structuredClone is built into the runtime
structuredClone throws DataCloneError on functions and DOM nodes—you need try/catch for safety

Imagine this: you're building a React application, and you need to create a snapshot of your user's settings before they make changes. You use JSON.parse(JSON.stringify(userSettings)), feeling confident in your battle-tested approach. Then the production bug reports start rolling in—dates are now strings, Maps are empty objects, and some users have data mysteriously missing.

This is the silent killer of JavaScript applications. The JSON cloning method you've used for years isn't just limited—it's actively losing your data. And the worst part? It fails silently, making debugging a nightmare. After helping hundreds of companies build reliable engineering teams, we've seen this exact bug destroy features in production systems.

The Shallow Copy Trap That's Destroying Your Applications

It starts innocently enough. You copy an object to avoid mutating the original, use the spread operator, and think you're protected. Then you modify a nested property and watch in horror as your "original" object changes too.

This is the shallow copy trap, and it's more common than you might think. We recently onboarded a team whose entire Redux state management was built on spread operators—every action was silently mutating the previous state. They had no idea until users started reporting inconsistent data across sessions.

The Spread Operator Problem

The spread operator creates shallow copies—only the first level is copied, while nested objects remain shared references:

const original = { name: "Sarah", address: { city: "Boston" } };
const copy = { ...original };
copy.address.city = "NYC";
console.log(original.address.city); // "NYC" — OOPS!

When your application scales, these hidden mutations compound. What works in development with simple data suddenly breaks in production with complex user profiles, settings objects, or state trees. The problem isn't your code—it's the tool you chose.

Struggling with complex data handling?

Boundev's dedicated teams handle data-intensive applications daily—building systems that correctly clone, serialize, and manage complex state without data loss.

See How We Build This

Why JSON.stringify Fails More Than You Think

For years, JSON.parse(JSON.stringify(obj)) was the standard deep cloning solution. It's fast, it's simple, and it works for simple objects. But here's what most developers don't realize: it's silently destroying your data in ways you might not notice until production.

We reviewed a client's legacy codebase last month and found 47 instances of JSON cloning. Every single one was losing data—Dates became strings, undefined properties vanished, and any Map or Set was completely stripped. They thought their application was working. It wasn't. It was silently degrading.

JSON.stringify() Loses:

Functions and methods
undefined values
Symbol properties
Map and Set objects
Circular references (throws error)
Date objects (becomes string)
RegExp objects (becomes empty object)

structuredClone() Handles:

All primitive types
Nested objects and arrays
Circular references
Map and Set
Date objects
RegExp objects
Typed Arrays and ArrayBuffer

The worst part? JSON.stringify doesn't throw errors when it loses your data—it just converts it to undefined or empty objects. Your application continues running with corrupted data, and you have no idea until users report issues.

Enter structuredClone: The Native Solution

JavaScript finally has a native answer. Introduced in Node.js 17 and modern browsers (Chrome 98+, Firefox 94+, Safari 15.4+), structuredClone() uses the same algorithm that powers web APIs like postMessage(). It's not a polyfill or library—it's built into the runtime.

After helping teams migrate from JSON cloning to structuredClone, we've seen immediate improvements: fewer bugs, smaller bundle sizes (no more Lodash), and data integrity that developers can trust. Here's what changed for one client who migrated their entire state management—zero data loss incidents in the following quarter.

1 Basic structuredClone Usage

Pass any object to structuredClone() and get a completely independent deep copy

2 Circular References

Objects that reference themselves are handled automatically—JSON.stringify throws an error

3 Complex Data Types

Map, Set, Date, RegExp, TypedArray—all preserved correctly

4 Zero Dependencies

No external libraries needed—eliminates Lodash dependency entirely

Ready to Build Your Remote Team?

Partner with Boundev to access pre-vetted developers who understand modern JavaScript patterns.

Talk to Our Team

When structuredClone Falls Short

No solution is perfect, and structuredClone has its limitations. Understanding these boundaries is critical for writing robust applications.

1

Functions Throw DataCloneError

If your object contains functions, structuredClone will throw an error. Always wrap in try/catch or sanitize your data first.

2

DOM Nodes Cause Errors

Attempting to clone DOM elements (like HTMLElement, Node, or Window) will throw. This matters for applications that serialize DOM state.

3

Prototype Chain Is Lost

The cloned object will be a plain object—class instances become plain objects with only own properties.

4

Getters and Setters Not Copied

Only the evaluated value is cloned—not the accessor properties themselves. The clone is a plain object snapshot.

For most modern applications, these limitations are actually features. You probably shouldn't be serializing functions anyway—it's a code smell that suggests your data model needs rethinking. When you need to preserve prototypes or custom logic, that's when you reach for specialized serialization libraries.

Making the Switch: Migration Strategy

Ready to move from JSON cloning to structuredClone? Here's how top engineering teams handle the migration without breaking production.

Need help with JavaScript modernization?

Boundev's staff augmentation model plugs senior developers directly into your team—accelerating your modernization without the months-long hiring process.

Learn How It Works

1 Audit Your Current Cloning

Search for JSON.parse(JSON.stringify), _.cloneDeep, and custom cloning functions

2 Wrap in Fallback

Create a safeClone() helper that uses structuredClone with JSON fallback for older environments

3 Test Edge Cases

Verify Date preservation, Map/Set handling, and error throwing with functions

4 Remove Dependencies

Once migrated, remove Lodash or other cloning libraries from your bundle

How Boundev Solves This for You

Everything we've covered in this blog—deep cloning complexities, data integrity challenges, and migration strategies—is exactly what our team handles every day. Here's how we approach it for our clients.

We build you a full remote engineering team—screened, onboarded, and shipping code in under a week. Your team gets senior developers who understand data integrity patterns.

● Pre-vetted for JavaScript expertise
● Experience with large-scale state management

Plug pre-vetted engineers directly into your existing team—no re-training, no culture mismatch, no delays.

● 48-hour matching
● JavaScript specialists available

Hand us the entire project. We manage architecture, development, and delivery—you focus on the business.

● Data integrity baked in
● Full-stack JavaScript expertise

The Bottom Line

17KB
Saved by removing Lodash
100%
Data type preservation
2022
When structuredClone landed
0
Dependencies needed

Frequently Asked Questions

Can I use structuredClone in production today?

Yes. structuredClone() is available in all modern browsers (Chrome 98+, Firefox 94+, Safari 15.4+) and Node.js 17+. For older environments, use a polyfill like @ungap/structured-clone. Most enterprise applications can safely use it—browser usage statistics show over 95% global support.

What's faster: JSON.stringify or structuredClone?

For simple objects without special types, JSON.stringify is about 2-3x faster. However, this speed comes at the cost of losing data—Dates become strings, undefined disappears, and Maps/Sets are destroyed. structuredClone is fast enough for most use cases (only 20-30% slower) and preserves your data integrity. For complex applications with user state, the performance difference is negligible compared to the data safety gains.

How do I handle cloning objects with functions?

First, ask yourself if functions in your data is a design issue. For most cases, strip functions before serialization. Create a helper function that sanitizes your data: const safeClone = (obj) => JSON.parse(JSON.stringify(obj)) for objects with functions, or use structuredClone with try/catch to catch the error and fallback gracefully. Ideally, refactor to keep functions separate from serializable data.

Should I remove Lodash from my project?

If you're only using Lodash for cloneDeep, yes—remove it. Lodash cloneDeep adds 17.4KB (5.3KB gzipped) to your bundle. structuredClone handles all the same cases plus circular references that Lodash struggles with. However, if you use many Lodash functions, evaluate the full bundle size impact. Many teams keep Lodash but remove the specific cloneDeep import.

Free Consultation

Let's Build This Together

You now know exactly what it takes to handle complex data in JavaScript. The next step is execution—and that's where Boundev comes in.

200+ companies have trusted us to build their engineering teams. Tell us what you need—we'll respond within 24 hours.

200+
Companies Served
72hrs
Avg. Team Deployment
98%
Client Satisfaction

Tags

#JavaScript#Deep Cloning#Structured Clone#Programming#Web Development#Software Development
B

Boundev Team

At Boundev, we're passionate about technology and innovation. Our team of experts shares insights on the latest trends in AI, software development, and digital transformation.

Ready to Transform Your Business?

Let Boundev help you leverage cutting-edge technology to drive growth and innovation.

Get in Touch

Start Your Journey Today

Share your requirements and we'll connect you with the perfect developer within 48 hours.

Get in Touch