JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 13657631
  • Score
    100M100P100Q210030F
  • License MIT

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

Package Exports

  • klona

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (klona) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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

Features

  • Super tiny and performant
  • Deep clone / recursive copies
  • Safety with Dates and RegExps

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.15.3

Validation:
  ✘ fast-clone (FAILED @ "intial copy")
  ✔ lodash
  ✔ clone-deep
  ✘ deep-copy (FAILED @ "intial copy")
  ✔ depcopy
  ✔ klona

Benchmark:
  fast-clone       x  22,323 ops/sec ±1.41% (90 runs sampled)
  lodash           x  39,066 ops/sec ±1.50% (90 runs sampled)
  clone-deep       x  81,136 ops/sec ±1.32% (91 runs sampled)
  deep-copy        x 109,054 ops/sec ±1.27% (94 runs sampled)
  depcopy          x  23,686 ops/sec ±0.75% (96 runs sampled)
  klona            x 238,643 ops/sec ±1.90% (92 runs sampled)
  • dlv – safely read from deep properties in 120 bytes
  • dset – safely write into deep properties in 160 bytes

License

MIT © Luke Edwards