Which is Better JavaScript Object Cloning Libraries?
clone vs rfdc vs fast-copy vs lodash.clone
1 Year
clonerfdcfast-copylodash.cloneSimilar Packages:
What's JavaScript Object Cloning Libraries?

Object cloning libraries in JavaScript provide developers with tools to create deep copies of objects, ensuring that modifications to the cloned object do not affect the original. These libraries vary in terms of performance, features, and use cases, catering to different needs in web development. They help in managing state immutability, optimizing performance, and ensuring data integrity when working with complex data structures. Understanding the nuances of each library can significantly enhance the efficiency of data manipulation tasks in applications.

NPM Package Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
clone31,679,527781-287 years agoMIT
rfdc18,701,82063927.1 kB154 months agoMIT
fast-copy3,442,6541,130198 kB47 months agoMIT
lodash.clone825,78759,705-1088 years agoMIT
Feature Comparison: clone vs rfdc vs fast-copy vs lodash.clone

Performance

  • clone: The 'clone' package is designed for simplicity and ease of use, but it may not be optimized for performance in scenarios involving large or deeply nested objects. It is suitable for basic cloning tasks without heavy performance demands.
  • rfdc: 'rfdc' excels in performance, providing a fast and memory-efficient cloning solution. It is particularly well-suited for performance-critical applications, making it a top choice for developers focused on speed.
  • fast-copy: 'fast-copy' is specifically engineered for high performance, utilizing optimized algorithms to handle large and complex objects efficiently. It is one of the fastest cloning libraries available, making it ideal for applications that require frequent cloning operations.
  • lodash.clone: While 'lodash.clone' offers reliable performance, it may not be as fast as 'fast-copy' for large objects. However, it provides a good balance between performance and usability, especially for those already using Lodash in their projects.

Deep Cloning

  • clone: The 'clone' package supports deep cloning, allowing for the creation of copies of nested objects. However, it may not handle certain complex data types, such as functions or special objects, as effectively as other libraries.
  • rfdc: 'rfdc' supports deep cloning and is particularly adept at handling complex data structures. It is designed to manage various data types effectively, ensuring that the cloned object mirrors the original accurately.
  • fast-copy: 'fast-copy' provides robust deep cloning capabilities, accurately copying nested objects, arrays, and handling various data types with high fidelity. It is designed to ensure that all aspects of the original object are preserved in the clone.
  • lodash.clone: 'lodash.clone' offers deep cloning functionality, but it may not cover all edge cases, such as circular references or specific object types. It is reliable for standard use cases but may require additional handling for complex structures.

Ease of Use

  • clone: The 'clone' package is straightforward and easy to use, making it accessible for developers of all skill levels. Its minimalistic approach allows for quick integration into projects without a steep learning curve.
  • rfdc: 'rfdc' is designed with simplicity in mind, offering a clean API that is easy to understand. Its minimalistic approach allows developers to quickly grasp its functionality and integrate it into their projects.
  • fast-copy: 'fast-copy' is user-friendly and provides a simple API, making it easy to implement in projects. However, its focus on performance may require developers to understand its nuances to fully leverage its capabilities.
  • lodash.clone: 'lodash.clone' integrates seamlessly with the Lodash library, providing a familiar API for those already using Lodash. Its extensive documentation and community support enhance its usability.

Data Integrity

  • clone: The 'clone' package ensures data integrity by creating copies that do not reference the original object. However, it may not handle all edge cases, potentially leading to unexpected behavior in complex scenarios.
  • rfdc: 'rfdc' prioritizes data integrity, ensuring that all aspects of the original object are preserved in the clone. It effectively manages complex structures, making it a trustworthy choice for maintaining data consistency.
  • fast-copy: 'fast-copy' guarantees high data integrity, accurately replicating the original object's structure and values. It is designed to handle various data types, ensuring that the cloned object remains consistent with the original.
  • lodash.clone: 'lodash.clone' provides reliable data integrity for standard use cases, ensuring that modifications to the cloned object do not affect the original. However, it may require additional handling for complex data types.

Library Size

  • clone: The 'clone' package is lightweight, making it a good choice for projects where minimizing bundle size is essential. Its simplicity contributes to a smaller footprint in applications.
  • rfdc: 'rfdc' is minimal in size, focusing on delivering fast cloning capabilities without additional overhead. Its small footprint makes it an attractive option for performance-conscious developers.
  • fast-copy: 'fast-copy' is also lightweight, designed for performance without unnecessary bloat. It strikes a balance between functionality and size, making it suitable for performance-sensitive applications.
  • lodash.clone: 'lodash.clone' is part of the larger Lodash library, which may increase bundle size if only cloning functionality is needed. However, its extensive features may justify the size for applications already using Lodash.
How to Choose: clone vs rfdc vs fast-copy vs lodash.clone
  • clone: Choose 'clone' for a straightforward and lightweight solution that handles basic cloning needs without additional dependencies or complexity. It is ideal for simple use cases where performance is not a critical concern.
  • rfdc: Use 'rfdc' for its focus on performance and minimalism, especially in environments where memory usage is a concern. It provides a fast cloning mechanism with a small footprint, making it suitable for performance-sensitive applications.
  • fast-copy: Opt for 'fast-copy' when performance is paramount, especially in scenarios involving large or deeply nested objects. It is designed for speed and efficiency, making it suitable for high-performance applications that require frequent cloning operations.
  • lodash.clone: Select 'lodash.clone' if you are already using the Lodash library and prefer a consistent API. It offers a reliable cloning method with additional utility functions, making it a good choice for projects that leverage Lodash for various data manipulation tasks.
README for clone

clone

build status downloads

offers foolproof deep cloning of objects, arrays, numbers, strings, maps, sets, promises, etc. in JavaScript.

XSS vulnerability detected

Installation

npm install clone

(It also works with browserify, ender or standalone. You may want to use the option noParse in browserify to reduce the resulting file size, since usually Buffers are not needed in browsers.)

Example

var clone = require('clone');

var a, b;

a = { foo: { bar: 'baz' } };  // initial value of a

b = clone(a);                 // clone a -> b
a.foo.bar = 'foo';            // change a

console.log(a);               // show a
console.log(b);               // show b

This will print:

{ foo: { bar: 'foo' } }
{ foo: { bar: 'baz' } }

clone masters cloning simple objects (even with custom prototype), arrays, Date objects, and RegExp objects. Everything is cloned recursively, so that you can clone dates in arrays in objects, for example.

API

clone(val, circular, depth)

  • val -- the value that you want to clone, any type allowed

  • circular -- boolean

    Call clone with circular set to false if you are certain that obj contains no circular references. This will give better performance if needed. There is no error if undefined or null is passed as obj.

  • depth -- depth to which the object is to be cloned (optional, defaults to infinity)

  • prototype -- sets the prototype to be used when cloning an object. (optional, defaults to parent prototype).

  • includeNonEnumerable -- set to true if the non-enumerable properties should be cloned as well. Non-enumerable properties on the prototype chain will be ignored. (optional, defaults to false)

clone.clonePrototype(obj)

  • obj -- the object that you want to clone

Does a prototype clone as described by Oran Looney.

Circular References

var a, b;

a = { hello: 'world' };

a.myself = a;
b = clone(a);

console.log(b);

This will print:

{ hello: "world", myself: [Circular] }

So, b.myself points to b, not a. Neat!

Test

npm test

Changelog

v2.1.2

2018-03-21

  • Use Buffer.allocUnsafe() on Node >= 4.5.0 (contributed by @ChALkeR)

v2.1.1

2017-03-09

  • Fix build badge in README
  • Add support for cloning Maps and Sets on Internet Explorer

v2.1.0

2016-11-22

  • Add support for cloning Errors
  • Exclude non-enumerable symbol-named object properties from cloning
  • Add option to include non-enumerable own properties of objects

v2.0.0

2016-09-28

  • Add support for cloning ES6 Maps, Sets, Promises, and Symbols

v1.0.3

2017-11-08

  • Close XSS vulnerability in the NPM package, which included the file test-apart-ctx.html. This vulnerability was disclosed by Juho Nurminen of 2NS - Second Nature Security.

v1.0.2 (deprecated)

2015-03-25

  • Fix call on getRegExpFlags
  • Refactor utilities
  • Refactor test suite

v1.0.1 (deprecated)

2015-03-04

  • Fix nodeunit version
  • Directly call getRegExpFlags

v1.0.0 (deprecated)

2015-02-10

  • Improve browser support
  • Improve browser testability
  • Move helper methods to private namespace

Caveat

Some special objects like a socket or process.stdout/stderr are known to not be cloneable. If you find other objects that cannot be cloned, please open an issue.

Bugs and Issues

If you encounter any bugs or issues, feel free to open an issue at github or send me an email to paul@vorba.ch. I also always like to hear from you, if you’re using my code.

License

Copyright © 2011-2016 Paul Vorbach and contributors.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.