CyrefJS
CCyrefJS
فا
Back to documentation
Object/Since 1.38.0

deepClone

Creates a deep clone of a value while preserving object prototypes, property descriptors, special built-in types, and circular references.

deepClone(value):T

Parameters

valueTrequired

The value to clone.

Returns

T

Returns a deep clone of the input value.

Errors

Throws a TypeError if the value contains a WeakMap.

Throws a TypeError if the value contains a WeakSet.

Examples

01

Basic usage

Creates an independent deep copy of a nested object.

TypeScript
const original = {
  name: "Alice",
  profile: {
    age: 25,
  },
};

const copy = deepClone(original);

copy.profile.age = 30;

console.log(original.profile.age);
// 25

console.log(copy.profile.age);
// 30
02

Nested arrays

Nested arrays are cloned recursively.

TypeScript
const original = [
  [1, 2],
  [3, 4],
];

const copy = deepClone(original);

copy[0][0] = 99;

console.log(original[0][0]);
// 1

console.log(copy[0][0]);
// 99
03

Date

Date instances are cloned with the same timestamp.

TypeScript
const original = new Date("2026-01-01");

const copy = deepClone(original);

console.log(copy.getTime() === original.getTime());
// true

console.log(copy === original);
// false
04

RegExp

RegExp instances preserve their source, flags, and lastIndex.

TypeScript
const original = /hello/gi;

original.lastIndex = 3;

const copy = deepClone(original);

console.log(copy.source);
// hello

console.log(copy.flags);
// gi

console.log(copy.lastIndex);
// 3
05

Map

Map keys and values are cloned recursively.

TypeScript
const original = new Map([
  ["user", { name: "Alice" }],
]);

const copy = deepClone(original);

copy.get("user")!.name = "Bob";

console.log(original.get("user")!.name);
// Alice

console.log(copy.get("user")!.name);
// Bob
06

Set

Set values are cloned recursively.

TypeScript
const original = new Set([
  { id: 1 },
]);

const copy = deepClone(original);

const clonedItem = [...copy][0];

clonedItem.id = 2;

console.log([...original][0].id);
// 1

console.log(clonedItem.id);
// 2
07

Circular references

Circular references are preserved without causing infinite recursion.

TypeScript
const original: {
  name: string;
  self?: unknown;
} = {
  name: "Node",
};

original.self = original;

const copy = deepClone(original);

console.log(copy !== original);
// true

console.log(copy.self === copy);
// true
08

Shared references

Multiple references to the same source object remain references to the same cloned object.

TypeScript
const shared = {
  value: 42,
};

const original = {
  first: shared,
  second: shared,
};

const copy = deepClone(original);

console.log(copy.first === copy.second);
// true

console.log(copy.first === shared);
// false
09

Prototype preservation

Objects are created using the prototype of the original object.

TypeScript
class User {
  constructor(public name: string) {}
}

const original = new User("Alice");
const copy = deepClone(original);

console.log(copy instanceof User);
// true

console.log(copy === original);
// false
10

Property descriptors

Own property descriptors are preserved while their values are cloned.

TypeScript
const original = {};

Object.defineProperty(original, "value", {
  value: { count: 1 },
  writable: false,
  enumerable: false,
  configurable: true,
});

const copy = deepClone(original);

const descriptor = Object.getOwnPropertyDescriptor(
  copy,
  "value",
);

console.log(descriptor?.writable);
// false

console.log(descriptor?.enumerable);
// false
11

Primitive values

Primitive values are returned directly because they do not require cloning.

TypeScript
console.log(deepClone(42));
// 42

console.log(deepClone("hello"));
// hello

console.log(deepClone(true));
// true

console.log(deepClone(null));
// null
12

WeakMap

WeakMap instances cannot be cloned and cause a TypeError.

TypeScript
const value = new WeakMap();

deepClone(value);
// TypeError: Cannot clone WeakMap
13

WeakSet

WeakSet instances cannot be cloned and cause a TypeError.

TypeScript
const value = new WeakSet();

deepClone(value);
// TypeError: Cannot clone WeakSet
Built with@cyref/js·Official documentation