Clone Deep provides recursive (deep) cloning functionality for JavaScript native types, including Object, Array, RegExp, Date, and all primitive values. It offers a simple, focused API for creating independent copies of complex nested data structures while preserving type information and object relationships.
npm install clone-deepconst cloneDeep = require('clone-deep');For ES modules:
import cloneDeep from 'clone-deep';const cloneDeep = require('clone-deep');
// Clone nested objects
const original = {
user: { name: 'Alice', preferences: { theme: 'dark' } },
items: [{ id: 1, active: true }, { id: 2, active: false }]
};
const copy = cloneDeep(original);
// Modifications to copy don't affect original
copy.user.name = 'Bob';
copy.items[0].active = false;
console.log(original.user.name); // 'Alice'
console.log(original.items[0].active); // trueClone Deep is built around type-aware recursive cloning:
kind-of library to identify value types (object, array, primitive, etc.)is-plain-object to identify objects suitable for deep cloningshallow-clone for primitives and non-cloneable objectsinstanceClone parameter for custom object cloning behaviorRecursively clones JavaScript native types with optional custom instance handling.
/**
* Recursively clone JavaScript native types
* @param {any} value - The value to clone (objects, arrays, primitives, etc.)
* @param {boolean|function} [instanceClone] - Controls cloning of non-plain objects
* @returns {any} - Deep clone of the input value
*/
function cloneDeep(value, instanceClone);Parameters:
value (any): The value to clone. Supports all JavaScript types including:
instanceClone (boolean|function, optional): Controls how non-plain objects are handled:
undefined or false: Copy object references for non-plain objects (default behavior)true: Deep clone all objects including constructor instancesfunction: Custom cloning function called for non-plain objectsReturns: Deep clone of the input value with the same type and structure
Usage Examples:
const cloneDeep = require('clone-deep');
// Basic object cloning
const obj = { a: 'b', nested: { x: 1, y: 2 } };
const cloned = cloneDeep(obj);
// obj and cloned are completely independent
// Array cloning with nested objects
const arr = [{ name: 'Alice' }, { name: 'Bob' }];
const clonedArr = cloneDeep(arr);
// Modifying clonedArr[0].name won't affect arr[0].name
// Primitive values
const str = cloneDeep('hello'); // 'hello'
const num = cloneDeep(42); // 42
const bool = cloneDeep(true); // true
// RegExp and Date objects
const regex = cloneDeep(/pattern/gi); // Independent RegExp copy
const date = cloneDeep(new Date()); // Independent Date copy
// Custom instance cloning
function CustomClass(value) {
this.value = value;
}
const instance = new CustomClass({ data: 'test' });
// Default: copy reference
const refCopy = cloneDeep(instance);
// Clone instances: deep clone the instance
const instanceCopy = cloneDeep(instance, true);
// Custom cloning function
const customCopy = cloneDeep(instance, function(obj) {
if (obj instanceof CustomClass) {
return new CustomClass(cloneDeep(obj.value));
}
return obj;
});Supported Data Types:
instanceClone behaviorType Safety:
The function preserves the type and structure of the input while ensuring complete independence between the original and cloned values. Circular references are not explicitly handled and may cause stack overflow errors.