klona - Deep Cloning Utility

2.0.6 · active · verified Sun Apr 19

klona is a minimalist, high-performance utility for deep cloning JavaScript objects, arrays, dates, regexps, and other complex data types. Currently stable at version 2.0.6, it maintains a regular release cadence with frequent patches and minor improvements, as seen with multiple patch releases since v2.0.0. Its primary differentiator lies in its tiny footprint (ranging from 240B for `klona/json` to 501B for `klona/full` gzipped) and superior speed compared to many alternatives. It offers multiple "modes" (`klona/json`, `klona/lite`, `klona`, `klona/full`), allowing developers to import only the necessary functionality for specific data type support, thereby optimizing bundle size. It supports ESM, CommonJS, and UMD, and ships with TypeScript types.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to import and use the `klona` function to perform a deep clone of a complex object, including nested objects, dates, regexps, sets, and maps, and verifies that modifications to the clone do not affect the original source object.

import { klona } from 'klona';

const input = {
  foo: 1,
  bar: {
    baz: 2,
    bat: {
      hello: 'world'
    }
  },
  date: new Date(),
  regex: /test/g,
  items: new Set([1, 2, 3]),
  mapData: new Map([['a', 1], ['b', 2]])
};

const output = klona(input);

// Verify deep equality (requires an assertion library like Node's assert.deepStrictEqual)
// import * as assert from 'assert';
// assert.deepStrictEqual(input, output);

// Modifying the clone does not affect the original
output.bar.bat.hola = 'mundo';
output.bar.baz = 99;
output.items.add(4);
output.date.setFullYear(2030);

console.log('Original Input:', JSON.stringify(input, null, 2));
console.log('Cloned Output (modified):', JSON.stringify(output, null, 2));

// You would see 'hola' and 'mundo' only in the output, and 'baz' will be 99.
// The date and set will also reflect the changes only in the output.

view raw JSON →