Skip to main content

deepCloneFull()

deepCloneFull<T>(value): T

Creates a deep clone of any JavaScript value, handling all types.

Supports all types from deepClone plus: TypedArrays, ArrayBuffer, SharedArrayBuffer, DataView, Buffer (Node.js), Blob, File, and boxed primitives. Preserves prototypes and symbol keys. Handles circular references. Functions are copied by reference (not cloned).

๐Ÿ’Ž Why is this a Hidden Gem?

A comprehensive cloning utility that handles binary attributes (Buffers, TypedArrays) and special objects (Date, RegExp, Map, Set).


Type Parametersโ€‹

T: Tโ€‹

The type of the destination object.


Parametersโ€‹

value: Tโ€‹

The value to clone deeply.


Returns: Tโ€‹

A deep clone of the input value.


Sinceโ€‹

2.0.0


Performanceโ€‹

O(n) where n is the total number of properties.


Examplesโ€‹

const obj = {
date: new Date(),
buffer: new Uint8Array([1, 2, 3]),
nested: { a: 1 }
};
const cloned = deepCloneFull(obj);
cloned.nested.a = 99;
obj.nested.a; // => 1 (unchanged)
const buffer = new ArrayBuffer(8);
const view = new DataView(buffer);
view.setInt32(0, 42);
const cloned = deepCloneFull(view);

How it works?โ€‹

Creates a deep clone of any JavaScript value, handling ALL types including TypedArrays, Buffers, Blobs, and boxed primitives.

Supported Types (vs deepClone)โ€‹

Circular Reference Handlingโ€‹

Performanceโ€‹

TypeComplexity
PrimitivesO(1)
Arrays/ObjectsO(n)
Circular refsO(1) lookup

Use Casesโ€‹

Clone complex data structures ๐Ÿ“Œโ€‹

Deep copy objects containing binary data, Dates, or Maps. Critical for specialized applications dealing with rich data types.

const message = {
id: 1,
payload: new Uint8Array([0x1, 0x2]),
timestamp: new Date()
};
const clone = deepCloneFull(message);

Duplicate Error objectsโ€‹

Create a copy of an Error object, preserving stack and name. Useful for logging or re-throwing errors with modifications.

const error = new Error('Something failed');
const copy = deepCloneFull(error);
copy.message = 'New message'; // Original is untouched

Copy Map/Set collectionsโ€‹

Deeply clone native collections while preserving their structure. Essential for applications heavily using Map or Set.

const map = new Map([['a', { v: 1 }]]);
const clone = deepCloneFull(map);
clone.get('a').v = 2; // Deep cloned, original { v: 1 } safe