Registry / serialization / nanoclone

nanoclone

JSON →
library1.0.2jsnpmunverified

nanoclone is a minimalist JavaScript utility designed for deep cloning objects, weighing in at approximately 300 bytes. As of version 1.0.2, it provides a highly efficient solution for creating independent copies of various data structures, including primitives, arrays, plain objects, DOM Nodes, Date and RegExp instances, as well as Maps and Sets. A key differentiator is its ability to handle circular structures without falling into infinite loops. The library focuses on performance and a small footprint, offering a viable alternative to larger cloning libraries, especially in performance-sensitive or size-constrained environments. While its release cadence isn't explicitly defined, its small and focused scope suggests updates are primarily for bug fixes or minor enhancements. It ships with TypeScript types, enhancing developer experience in TypeScript projects.

npm install nanoclone
INSTALL
IMPORT
SIG · NANOCLONE
N
nanoclone
serializationjavascriptv1.0.2
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

clone
✓ import clone from 'nanoclone'
✗ import { clone } from 'nanoclone'
The `clone` function is exported as the default export for ESM environments.
clone
✓ const clone = require('nanoclone')
✗ const { clone } = require('nanoclone')
For CommonJS environments, `nanoclone` exports the `clone` function as the `module.exports` default.
clone (TypeScript type)
✓ import type clone from 'nanoclone'
When using TypeScript, the type of the `clone` function can be imported for type declarations.

This quickstart demonstrates how to import and use `nanoclone` to perform a deep copy of a complex JavaScript object, including nested structures and a Date instance, verifying that the cloned object is independent of the original.

import clone from 'nanoclone'; interface DeepObject { num: number; arr: number[]; nested: { obj: { a: number; }; }; d?: Date; } let a: DeepObject = { num: 2, arr: [1, 2, 3], nested: { obj: { a: 0 } }, d: new Date() }; let b: DeepObject = clone(a); // Verify independence a.num = 10; // Change original b.nested.obj.a = 5; // Change clone a.d?.setFullYear(2000); // Change original Date console.log('Original object:', a); // Expected: { num: 10, arr: [1, 2, 3], nested: { obj: { a: 0 } }, d: Date(2000...) } console.log('Cloned object:', b); // Expected: { num: 2, arr: [1, 2, 3], nested: { obj: { a: 5 } }, d: Date(current year...) }
Debug
Known issues
gotchananoclone provides deep cloning for primitives, arrays, plain objects, Date, RegExp, Map, Set, and DOM Nodes. However, it does not explicitly support or correctly clone all complex built-in JavaScript objects (e.g., WeakMap, WeakSet, ArrayBuffer, TypedArrays, Promise, Error instances) or instances of user-defined classes. These types may be shallow copied, return an empty object, or not preserve internal state.
fix
For unsupported complex types, consider manually handling cloning for those specific parts (e.g., using `JSON.parse(JSON.stringify(obj))` for JSON-safe data) or using a more feature-rich (and larger) deep cloning library if comprehensive type support is critical.
affects: >=1.0.0
gotchaWhile nanoclone handles circular references, deep cloning can be computationally intensive for extremely large, deeply nested, or highly interconnected objects. This can impact performance or potentially lead to 'Maximum call stack size exceeded' errors in environments with limited stack sizes.
fix
Profile application performance when cloning large datasets. Consider optimizing object structures, shallow cloning less critical parts, or implementing custom cloning logic for specific performance bottlenecks.
affects: >=1.0.0
gotchaFunctions and Symbols are copied by reference, not deeply cloned. This is standard behavior for deep cloning libraries as cloning executable code or unique symbols is complex and rarely desired. Any functions or symbols within the object will point to the same original reference.
fix
Be aware that modifying properties of a function or a Symbol object in the original would reflect in the cloned object. This is generally the expected and desired behavior, but it's crucial to understand for mutable function properties.
affects: >=1.0.0
Errors
Common errors & fixes
Maximum call stack size exceeded
Attempting to clone an extremely deep object structure or a very complex, recursive object graph that exhausts the JavaScript engine's call stack during the recursive cloning process.
fix
Reduce the depth of the object being cloned, or consider custom cloning logic to manage recursion. While `nanoclone` handles circular structures, extreme depth can still be problematic.
Cloned object of type X does not behave as expected or is empty
`nanoclone` does not provide full deep cloning support for all possible JavaScript object types beyond its explicitly listed supported types (e.g., WeakMap, Promises, custom class instances). For these types, it might perform a shallow copy, return an empty object, or fail to preserve their internal state.
fix
Review the 'Supported' list in `nanoclone`'s documentation. If you need to clone unsupported types, you may need to implement custom serialization/deserialization for those specific parts of your object or use a different cloning library that offers broader type support.
Upgrade
Version history
1.0.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
7 hits · last 30 days
node
6
Amazon
1
Resources
nanoclone — npm install nanoclone · libregistry