Detalhes do pacote

@visulima/deep-clone

visulima641MIT2.1.10

Fastest deep clone implementation.

anolilab, clone, clone-deep, copy

readme (leia-me)

Visulima Deep Clone

Really Fast Deep Clone.


typescript-image npm-image license-image

Daniel Bannert's open source work is supported by the community on GitHub Sponsors


Install

npm install @visulima/deep-clone
yarn add @visulima/deep-clone
pnpm add @visulima/deep-clone

Usage

Copy or deep clone an input value to an arbitrary depth. The function accepts both objects and primitives.

import deepClone from "@visulima/deep-clone";

const cloned = deepClone({ a: 1, b: { c: 2 } });

console.log(cloned); // => {a: 1, b: {c: 2}}

API

deepClone(input, options?)

input

Type: any

The input value to copy.

options

Type: object

strict

Type: boolean

Default: false

If true, it will copy all properties, including non-enumerable ones and symbols.

handlers

Type: object

A set of custom handlers for specific type of value. Each handler is a function that takes the original value and returns a new value or throws an error if the value is not supported.

  • Array: InternalHandler<unknown[]>;
  • ArrayBuffer: InternalHandler<ArrayBuffer>;
  • Blob: InternalHandler<Blob>;
  • DataView: InternalHandler<DataView>;
  • Date: InternalHandler<Date>;
  • Error: InternalHandler<Error>;
  • Float32Array: InternalHandler<Float32Array>;
  • Float64Array: InternalHandler<Float64Array>;
  • Int8Array: InternalHandler<Int8Array>;
  • Int16Array: InternalHandler<Int16Array>;
  • Int32Array: InternalHandler<Int32Array>;
  • Map: InternalHandler<Map<unknown, unknown>>;
  • Object: InternalHandler<Record<string, unknown>>;
  • Promise: InternalHandler<Promise<unknown>>;
  • RegExp: InternalHandler<RegExp>;
  • Set: InternalHandler<Set<unknown>>;
  • WeakMap: InternalHandler<WeakMap<any, unknown>>;
  • WeakSet: InternalHandler<WeakSet<any>>;

Utils

copyOwnProperties(input)

Copy all properties contained on the object.

import { copyOwnProperties } from "@visulima/deep-clone/utils";

const obj = { a: 1, b: 2 };

const copy = copyOwnProperties(obj);

console.log(copy); // => {a: 1, b: 2}

getCleanClone(input)

Get an empty version of the object with the same prototype it has.

import { getCleanClone } from "@visulima/deep-clone/utils";

const obj = { a: 1, b: 2 };

const clean = getCleanClone(obj);

console.log(clean); // => {}

Notes

  • List of supported values/types:

    • undefined (original value is returned)
    • null (original value is returned)
    • boolean/Boolean (original value is returned)
    • string/String (original value is returned)
    • number/Number (original value is returned)
    • function
    • Object
    • Date
    • RegExp
    • Set
    • Map
    • Error
    • URIError
    • ReferenceError
    • SyntaxError
    • RangeError
    • EvalError
    • TypeError
    • System Error (Node.js)
    • Array
    • Int8Array
    • Uint8Array
    • Uint8ClampedArray
    • Init16Array
    • Uint16Array
    • Int32Array
    • Uint32Array
    • Float32Array
    • Float64Array
    • Buffer (Node.js)
    • DataView
    • Blob
  • List of unsupported values/types:

    • DOMElement: to copy DOM elements, use element.cloneNode().
    • Symbol
    • WeakMap
    • WeakSet
    • File
    • FileList
    • ImageData
    • ImageBitmap
    • Promise
    • SharedArrayBuffer
  • The implementation can handle circular references.

  • If a Number, String, or Boolean object is encountered, the value is cloned as a primitive. This behavior is intentional. The implementation is opinionated in wanting to avoid creating numbers, strings, and booleans via the new operator and a constructor.
  • The implementation only checks whether basic Objects, Arrays, and class instances are extensible, sealed, and/or frozen.
  • functions are not cloned; their reference is copied.
  • The implementation supports custom error types which are Error instances (e.g., ES2015 subclasses).

Benchmarks

Note:

It is true that jsondiffpatch.clone() from jsondiffpatch is faster than @visulima/deep-clonse in this particular benchmark, but it cannot handle as many situations as @visulima/deep-clonse can.

It is true that fastest-json-copy is faster than @visulima/deep-clonse in this particular benchmark. Also, fastest-json-copy has such huge limitations that it is rarely useful. For example, it treats things like Date and Map instances the same as empty {}. It can't handle circular references.

plain-object-clone is also really limited in capability.

Supported Node.js Versions

Libraries in this ecosystem make the best effort to track Node.js’ release schedule. Here’s a post on why we think this is important.

Contributing

If you would like to help take a look at the list of issues and check our Contributing guild.

Note: please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

Credits

License

The visulima deep-clone is open-sourced software licensed under the MIT

changelog (log de mudanças)

@visulima/deep-clone 2.1.10 (2025-03-07)

Bug Fixes

  • updated @visulima/packem and other dev deps, for better bundling size (e940581)

Miscellaneous Chores

  • updated dev dependencies (487a976)

@visulima/deep-clone 2.1.9 (2025-01-25)

Bug Fixes

  • fixed wrong node version range in package.json (4ae2929)

Miscellaneous Chores

  • fixed typescript url (fe65a8c)
  • updated all dev dependencies (37fb298)
  • updated all dev dependencies and all dependencies in the app folder (87f4ccb)

@visulima/deep-clone 2.1.8 (2025-01-12)

Bug Fixes

  • updated @visulima/packem, and all other dev dependencies (7797a1c)

Miscellaneous Chores

  • updated dev dependencies (9de2eab)

@visulima/deep-clone 2.1.7 (2024-12-12)

Bug Fixes

  • added missing placeholder variables into LICENSE.md file (cef32c6)
  • allow node v23 (8ca929a)
  • allowed node 23, updated dev dependencies (f99d34e)
  • updated packem to v1.8.2 (23f869b)
  • updated packem to v1.9.2 (47bdc2d)

Styles

Miscellaneous Chores

  • updated dev dependencies (a916944)

@visulima/deep-clone 2.1.6 (2024-10-05)

Bug Fixes

  • updated dev dependencies, updated packem to v1.0.7, fixed naming of some lint config files (c071a9c)

@visulima/deep-clone 2.1.5 (2024-09-24)

Bug Fixes

  • update packem to v1 (05f3bc9)
  • updated esbuild from v0.23 to v0.24 (3793010)

Miscellaneous Chores

  • updated dev dependencies (05edb67)

@visulima/deep-clone 2.1.4 (2024-09-12)

Bug Fixes

  • deep-clone: moved from tsup to packem (5b40fb9)
  • deep-clone: moved from tsup to packem (5d11363)

Miscellaneous Chores

  • changed typescript version back to 5.4.5 (55d28bb)
  • clean up (0c6a87d)
  • update dev dependencies (0738f98)
  • updated all dev deps (ef143ce)
  • updated dev dependencies (45c2a76)
  • updated dev dependencies (da46d8e)
  • updated dev dependencies (ac67ec1)
  • updated dev dependencies (c889486)
  • updated dev dependencies and sorted the package.json (9571572)

Build System

  • fixed found audit error, updated all dev package deps, updated deps in apps and examples (4c51950)

@visulima/deep-clone 2.1.3 (2024-06-06)

Bug Fixes

Miscellaneous Chores

  • changed semantic-release-npm to pnpm (b6d100a)
  • deps: updated dev deps (d91ac38)
  • downgrade eslint-plugin-vitest (0162771)
  • update dev dependencies (09c4854)
  • updated dev dependencies (a2e0504)
  • updated dev dependencies (abd319c)
  • updated dev dependencies (0767afe)
  • updated dev dependencies (d7791e3)
  • updated dev dependencies (6005345)
  • updated dev dependencies (87dee15)
  • updated dev dependencies (bf2c635)
  • updated dev dependencies (f67c7f1)

@visulima/deep-clone 2.1.2 (2024-03-27)

Bug Fixes

  • added missing os key to package.json (4ad1268)

@visulima/deep-clone 2.1.1 (2024-03-04)

Bug Fixes

  • fixed all found type issues (eaa40d1)
  • minifyWhitespace on prod build, removed @tsconfig/* configs (410cb73)

@visulima/deep-clone 2.1.0 (2024-02-13)

Features

  • exported all deep-clone handler (bc01ec0)

@visulima/deep-clone 2.0.0 (2024-02-10)

⚠ BREAKING CHANGES

  • refactored deep-clone, added new handler option for overwrite our internal cloner

Signed-off-by: prisis d.bannert@anolilab.de

Features

  • refactored deep-clone to be 5 time faster in a loose version (41b3604)
  • refactored deep-clone, added new handler option for overwrite our internal cloner, created a loose and a strict version (c641f2d)

@visulima/deep-clone 1.0.8 (2024-01-19)

Bug Fixes

  • updated all deps, updated test based on eslint errors (909f8f3)

@visulima/deep-clone 1.0.7 (2023-11-30)

Bug Fixes

@visulima/deep-clone 1.0.6 (2023-11-30)

Bug Fixes

@visulima/deep-clone 1.0.5 (2023-11-07)

Bug Fixes

  • fixed the homepage url of the package (02075ce)

@visulima/deep-clone 1.0.4 (2023-11-02)

Bug Fixes

@visulima/deep-clone 1.0.3 (2023-10-30)

Bug Fixes

  • update dependencies in pnpm-lock.yaml (d3a5626)

@visulima/deep-clone 1.0.2 (2023-10-24)

Bug Fixes

@visulima/deep-clone 1.0.1 (2023-10-17)

Bug Fixes

@visulima/deep-clone 1.0.0 (2023-10-16)

Features