What is nanoclone?
The nanoclone npm package is a lightweight utility for deep cloning JavaScript objects. It is designed to be simple and efficient, making it a useful tool for copying objects without retaining references to the original objects.
What are nanoclone's main functionalities?
Deep Cloning
This feature allows you to create a deep copy of an object, ensuring that nested objects are also cloned and not just referenced.
const clone = require('nanoclone');
const original = { a: 1, b: { c: 2 } };
const copy = clone(original);
console.log(copy); // { a: 1, b: { c: 2 } }
console.log(copy.b === original.b); // false
Other packages similar to nanoclone
lodash.clonedeep
Lodash's `cloneDeep` function provides similar functionality for deep cloning objects. It is part of the larger Lodash utility library, which offers a wide range of functions for manipulating and traversing data structures. Compared to nanoclone, lodash.clonedeep is more feature-rich but also larger in size.
rfdc
The `rfdc` (Really Fast Deep Clone) package is another alternative for deep cloning objects. It is designed to be extremely fast and efficient, making it a good choice for performance-critical applications. Like nanoclone, it focuses solely on deep cloning, but it is optimized for speed.
clone-deep
The `clone-deep` package is another utility for deep cloning objects. It supports cloning of various data types, including arrays, buffers, and more. It is more versatile than nanoclone but also slightly larger in size.
nanoclone
Only 300B to deep clone JavaScript objects
Is it small enough?
See the size of the most popular clone npm package with 250M+ downloads
And nanoclone size:
Supported
Installation
yarn add nanoclone
npm install nanoclone
Usage
import clone from 'nanoclone'
let a = {
num: 2,
arr: [1, 2, 3],
nested: {
obj: {
a: 0
}
}
}
let b = clone(a)
License
MIT