Lodash _.cloneDeep() method

Beginner
⏱️ 7 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

What you’ll learn

  • How _.cloneDeep(value) recursively copies nested arrays and objects.
  • How deep clone avoids accidental mutation leaks across shared references.
  • When deep cloning is helpful and when it may be unnecessary overhead.
  • How to compare clone and cloneDeep behavior with practical examples.

Prerequisites

Understand object/array references and review _.clone() for shallow-copy behavior first.

  • You know that objects and arrays are reference types in JavaScript.
  • You can run snippets in Node.js or the browser Try-it editor.

Overview

_.cloneDeep walks through nested structures and builds a fully detached copy. It is ideal when you need safe mutation of deep fields without touching original data.

Deep isolation

Nested objects and arrays are copied, not shared.

Safer updates

Useful for state transforms where deep mutation should not leak.

More expensive

Deep cloning traverses full graphs, so use only when needed.

Syntax

javascript
_.cloneDeep(value)
  • value: any clonable JavaScript value.
  • Returns: a recursively cloned copy with detached nested references.
1

Deep copy nested object

Mutating a nested object in the clone does not affect the original.

javascript
import cloneDeep from "lodash/cloneDeep";

var source = { profile: { name: "Ava" } };
var copy = cloneDeep(source);
copy.profile.name = "Liam";

console.log(source.profile.name); // "Ava"
Try it Yourself
2

Deep copy nested array

Deep clone detaches nested array entries from the source.

javascript
import cloneDeep from "lodash/cloneDeep";

var source = [{ tags: ["a", "b"] }];
var copy = cloneDeep(source);
copy[0].tags.push("c");

console.log(source[0].tags); // ["a", "b"]
Try it Yourself
3

clone vs cloneDeep

Compare shallow and deep behavior on the same nested object.

javascript
import clone from "lodash/clone";
import cloneDeep from "lodash/cloneDeep";

var source = { cfg: { mode: "light" } };
var shallow = clone(source);
var deep = cloneDeep(source);

shallow.cfg.mode = "dark";
deep.cfg.mode = "solarized";
Try it Yourself

📋 _.cloneDeep vs _.clone

MethodNested refsPerformance
_.cloneDeep(value)DetachedHigher cost on big graphs
_.clone(value)SharedUsually faster for shallow needs

Pitfalls to avoid

Cost

Deep cloning everything

Repeated deep clones in hot paths can hurt performance and memory usage.

Scope

Ignoring data size

Very large object graphs can make cloneDeep expensive. Clone only what you need.

Choice

Using cloneDeep by default

Pick _.clone for top-level immutability; reserve deep clone for nested mutation safety.

❓ FAQ

No. It creates a separate deep copy and leaves the source unchanged.
cloneDeep recursively copies nested values, while clone only copies the top level.
Yes. Deep cloning traverses entire object graphs and can be expensive for large or deeply nested data.
Use shallow clone when only top-level replacement is required and nested data will not be mutated.

Summary

  • Purpose: _.cloneDeep creates a deep copy with detached nested references.
  • Trade-off: safer deep mutation at higher runtime cost.
  • Next: continue to Lodash _.cloneDeepWith().
Did you know?

_.cloneDeep recursively copies nested structures, so mutations in the clone do not affect the original tree.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful