deepClone

Deep clone any value, preferring structuredClone with a fallback that handles circular references, dates, maps, sets, and typed arrays.

Usage

The deepClone function deep-clones any JavaScript value.

It prefers the native structuredClone when available, falling back to a custom implementation for unsupported environments, with dedicated handling for circular references and built-in types.

import { deepClone } from '@movk/core'

const source = { a: 1, d: new Date(), m: new Map([[1, { x: 2 }]]) }
const cloned = deepClone(source)
cloned !== source // true
cloned.d !== source.d // true
cloned.m !== source.m // true
cloned.m.get(1) !== source.m.get(1) // true

API

deepClone<T>(obj, cache?)

Deep-clones any JavaScript value.

Parameters

obj
T required
The value to deep-clone.
cache
WeakMap<object, any>
An internal WeakMap used for circular reference memoization. You rarely need to pass this manually.

Returns

returns
T
A new deep-cloned value structurally equal to the input but with independent references.

Changelog

No recent changes
Copyright © 2024 - 2026 YiXuan - MIT License