{"id":15143,"library":"nanoclone","title":"nanoclone","description":"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.","status":"active","version":"1.0.2","language":"javascript","source_language":"en","source_url":"https://github.com/kelin2025/nanoclone","tags":["javascript","clone","deep","nano","nanoclone","deepclone","typescript"],"install":[{"cmd":"npm install nanoclone","lang":"bash","label":"npm"},{"cmd":"yarn add nanoclone","lang":"bash","label":"yarn"},{"cmd":"pnpm add nanoclone","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The `clone` function is exported as the default export for ESM environments.","wrong":"import { clone } from 'nanoclone'","symbol":"clone","correct":"import clone from 'nanoclone'"},{"note":"For CommonJS environments, `nanoclone` exports the `clone` function as the `module.exports` default.","wrong":"const { clone } = require('nanoclone')","symbol":"clone","correct":"const clone = require('nanoclone')"},{"note":"When using TypeScript, the type of the `clone` function can be imported for type declarations.","symbol":"clone (TypeScript type)","correct":"import type clone from 'nanoclone'"}],"quickstart":{"code":"import clone from 'nanoclone';\n\ninterface DeepObject {\n  num: number;\n  arr: number[];\n  nested: {\n    obj: {\n      a: number;\n    };\n  };\n  d?: Date;\n}\n\nlet a: DeepObject = {\n  num: 2,\n  arr: [1, 2, 3],\n  nested: {\n    obj: {\n      a: 0\n    }\n  },\n  d: new Date()\n};\n\nlet b: DeepObject = clone(a);\n\n// Verify independence\na.num = 10; // Change original\nb.nested.obj.a = 5; // Change clone\na.d?.setFullYear(2000); // Change original Date\n\nconsole.log('Original object:', a); \n// Expected: { num: 10, arr: [1, 2, 3], nested: { obj: { a: 0 } }, d: Date(2000...) }\nconsole.log('Cloned object:', b);  \n// Expected: { num: 2, arr: [1, 2, 3], nested: { obj: { a: 5 } }, d: Date(current year...) }\n","lang":"typescript","description":"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."},"warnings":[{"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.","message":"nanoclone 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"While 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"Functions 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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"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.","cause":"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.","error":"Maximum call stack size exceeded"},{"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.","cause":"`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.","error":"Cloned object of type X does not behave as expected or is empty"}],"ecosystem":"npm"}