Skip to main content

cloneDeep()

cloneDeep<T>(value): T

Creates a deep clone of a value using the native structuredClone API.

DEPRECATED

Use the native structuredClone() function directly instead.


Type Parametersโ€‹

T: Tโ€‹

The type of the value to clone.


Parametersโ€‹

value: Tโ€‹

The value to clone deeply.


Returns: Tโ€‹

A deep clone of the input value.


See Alsoโ€‹


Sinceโ€‹

2.0.0


Also known asโ€‹

clone (Remeda, Ramda) ยท cloneDeep (Lodash, es-toolkit, Radashi) ยท โŒ (Effect, Modern Dash, Antfu)


Exampleโ€‹

const original = { a: 1, b: { c: 2 } };

// โŒ Deprecated approach
const cloned = cloneDeep(original);
cloned.b.c = 3;
console.log(original.b.c); // 2 (unchanged)

// โœ… Recommended approach
const clonedNative = structuredClone(original);
clonedNative.b.c = 3;
console.log(original.b.c); // 2 (unchanged)

How it works?โ€‹

Creates a deep clone of a value. Deprecated: Use structuredClone() directly (ES2022) or Arkhe's deepClone.

Native Equivalentโ€‹

// โŒ cloneDeep(obj)
// โœ… structuredClone(obj)
// โœ… deepClone(obj) // Arkhe

Use Casesโ€‹

Deep copy object ๐Ÿ“Œโ€‹

Create deep copy of object.

structuredClone(obj);
// or
JSON.parse(JSON.stringify(obj));

Immutable updatesโ€‹

Clone before modifying.

const newState = structuredClone(state);
newState.user.name = "New Name";

Isolate dataโ€‹

Create independent copy.

const copy = structuredClone(original);
// Modifications don't affect original