Socket
Book a DemoInstallSign in
Socket

@d-fischer/klona

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@d-fischer/klona

A tiny (366B) and fast utility to "deep clone" Objects, Arrays, Dates, RegExps, and more!

latest
Source
npmnpm
Version
1.1.5
Version published
Maintainers
1
Created
Source
klona
version status downloads codecov
A tiny (366B) and fast utility to "deep clone" Objects, Arrays, Dates, RegExps, and more!

Features

  • Super tiny and performant
  • Deep clone / recursive copies
  • Safely handles complex data types
    Array, Date, Map, Object, RegExp, Set, TypedArray

Unlike a "shallow copy" (eg, Object.assign), a "deep clone" recursively traverses a source input and copies its values — instead of references to its values — into a new instance of that input. The result is a structurally equivalent clone that operates independently of the original source and controls its own values.

Additionally, this module is delivered as:

Why "klona"? It's "clone" in Swedish.
What's with the sheep? Dolly.

Install

$ npm install --save klona

Usage

import klona from 'klona';

const input = {
  foo: 1,
  bar: {
    baz: 2,
    bat: {
      hello: 'world'
    }
  }
};

const output = klona(input);

// exact copy of original
assert.deepStrictEqual(input, output);

// applying deep updates...
output.bar.bat.hola = 'mundo';
output.bar.baz = 99;

// ...doesn't affect source!
console.log(
  JSON.stringify(input, null, 2)
);
// {
//   "foo": 1,
//   "bar": {
//     "baz": 2,
//     "bat": {
//       "hello": "world"
//     }
//   }
// }

API

klona(input)

Returns: typeof input

Returns a deep copy/clone of the input.

Benchmarks

via Node.js v10.13.0

Validation:
  ✘ JSON.stringify (FAILED @ "initial copy")
  ✘ fast-clone (FAILED @ "initial copy")
  ✔ lodash
  ✔ clone-deep
  ✘ deep-copy (FAILED @ "initial copy")
  ✔ depcopy
  ✔ klona

Benchmark:
  JSON.stringify   x  37,803 ops/sec ±0.68% (89 runs sampled)
  fast-clone       x  24,210 ops/sec ±0.81% (91 runs sampled)
  lodash           x  40,563 ops/sec ±1.10% (94 runs sampled)
  clone-deep       x  85,020 ops/sec ±0.17% (95 runs sampled)
  deep-copy        x 116,139 ops/sec ±0.29% (96 runs sampled)
  depcopy          x  24,392 ops/sec ±0.71% (96 runs sampled)
  klona            x 274,496 ops/sec ±0.15% (99 runs sampled)
  • dlv – safely read from deep properties in 120 bytes
  • dset – safely write into deep properties in 160 bytes
  • dequal – safely check for deep equality in 247 bytes

License

MIT © Luke Edwards

Keywords

clone

FAQs

Package last updated on 30 Apr 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts