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 nanocloneVerified import paths — ran on the pinned version, not inferred.
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.
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.
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.
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.
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.
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.
No dependency data recorded yet.