jsondecode.com logo

HomeChevronBlogChevronJSON Deep Copy: All Methods Compared

Blog post

JSON Deep Copy: All Methods Compared

Compare all JavaScript deep copy methods: JSON.parse/stringify, structuredClone, Lodash cloneDeep, and recursive approaches.

author

Shashank Jain

Author

14/06/20261 minute 0 seconds read
JSON Deep Copy: All Methods ComparedJSON Deep Copy: All Methods Compared

Article

The Deep Copy Problem

In JavaScript, objects are passed by reference. Assigning an object copies the reference, not the value. Deep copying creates a completely independent clone with no shared references.

Method 1: JSON.parse + JSON.stringify

const clone = JSON.parse(JSON.stringify(original));

Pros: No dependencies, fast for plain data.

Cons: Loses undefined, functions, Date objects (become strings), RegExp, Map, Set, BigInt. Throws on circular references.

Method 2: structuredClone (Modern)

const clone = structuredClone(original);

Pros: Built-in, handles Date, Map, Set, ArrayBuffer, circular references. No dependencies.

Cons: Drops functions and prototypes. Requires Node.js 17+ or modern browsers.

Method 3: Lodash cloneDeep

const _ = require('lodash');
const clone = _.cloneDeep(original);

Pros: Handles virtually everything including custom class instances.

Cons: Adds lodash as a dependency.

Comparison Table

MethodDatesFunctionsMap/SetCircular
JSON parse/stringifyStringDroppedDroppedThrows
structuredCloneClonedThrowsClonedHandled
Lodash cloneDeepClonedClonedClonedHandled

FAQ

When should I use JSON.parse/stringify for cloning?

When your data is plain JSON-serializable (no Dates, Maps, or functions) — it is the simplest and fastest option. Common for cloning API responses, Redux state snapshots, or config objects.

Is structuredClone available in all browsers?

As of 2022: Chrome 98+, Firefox 94+, Safari 15.4+, Node.js 17+. Polyfill with core-js for older environments.

What is the fastest deep clone for large objects?

JSON.parse/JSON.stringify is often fastest for large plain objects. structuredClone is close. Benchmark your specific data shape.

Can I deep clone class instances?

Only Lodash cloneDeep or a custom recursive function preserves prototype methods. structuredClone and JSON both lose class methods.

How do I detect circular references?

Try JSON.stringify in a try/catch — it throws if circular references exist.

Keep reading

Recent blogs

View all

If jsondecode.com saved you time, share it with your team

Free forever. No ads. No sign-up. Help other developers find it.