Which is Better JavaScript Deep Cloning Libraries?
clone vs rfdc vs immer vs lodash.clonedeep vs fast-copy
1 Year
clonerfdcimmerlodash.clonedeepfast-copySimilar Packages:
What's JavaScript Deep Cloning Libraries?

Deep cloning libraries in JavaScript are designed to create a complete copy of an object, including all nested objects, ensuring that changes to the clone do not affect the original object. These libraries vary in performance, mutability handling, and additional features, making them suitable for different use cases in web development. Understanding their unique characteristics can help developers choose the right tool for their specific needs, whether it's for state management, data manipulation, or performance optimization.

NPM Package Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
clone33,631,148781-287 years agoMIT
rfdc19,995,24863927.1 kB164 months agoMIT
immer11,659,04627,630627 kB496 months agoMIT
lodash.clonedeep10,996,80559,738-1088 years agoMIT
fast-copy3,546,6461,133198 kB47 months agoMIT
Feature Comparison: clone vs rfdc vs immer vs lodash.clonedeep vs fast-copy

Performance

  • clone: The 'clone' library is straightforward and performs adequately for small to medium-sized objects. However, it may not be optimized for large datasets or complex structures, leading to slower performance in those cases.
  • rfdc: 'rfdc' (Really Fast Deep Clone) is known for its speed and efficiency. It is designed to handle deep cloning with minimal overhead, making it one of the fastest libraries available, especially for large or deeply nested objects.
  • immer: 'immer' is primarily focused on immutability rather than cloning per se. It allows for efficient state updates by using a proxy-based approach, which can be faster than traditional cloning methods when working with complex state management.
  • lodash.clonedeep: 'lodash.clonedeep' is part of the Lodash library and is highly optimized for performance. It handles circular references and complex objects well, making it a robust choice for deep cloning in performance-sensitive applications.
  • fast-copy: 'fast-copy' is designed for high performance and is particularly effective with large objects. It utilizes a more efficient algorithm that minimizes overhead, making it one of the fastest options available for deep cloning.

Mutability Handling

  • clone: The 'clone' library creates a new object that is entirely separate from the original, ensuring that any changes made to the clone do not affect the original object. However, it does not provide any built-in mechanisms for immutability.
  • rfdc: 'rfdc' guarantees that the original object remains untouched after cloning, providing a clean and efficient way to work with deep copies without affecting the source.
  • immer: 'immer' takes a different approach by allowing you to work with mutable code while maintaining immutability under the hood. It uses a proxy to track changes and only applies them to the state when finalized, making it ideal for state management in React applications.
  • lodash.clonedeep: 'lodash.clonedeep' ensures that the original object remains unchanged after cloning. It is designed to handle complex data structures while maintaining the integrity of the original object.
  • fast-copy: 'fast-copy' also creates a completely new object, ensuring mutability is respected. It is particularly effective at preserving the original object's structure while allowing for deep modifications to the clone.

Ease of Use

  • clone: The 'clone' library is very easy to use with a simple API, making it accessible for developers who need quick and straightforward cloning capabilities without much configuration.
  • rfdc: 'rfdc' offers a simple API that is easy to use, making it a good choice for developers looking for a fast cloning solution without complex configurations.
  • immer: 'immer' has a slightly steeper learning curve due to its unique approach to immutability. However, once understood, it provides a powerful way to manage state changes in a more intuitive manner.
  • lodash.clonedeep: 'lodash.clonedeep' is part of the Lodash suite, which many developers are already familiar with. Its usage is straightforward for those accustomed to Lodash's syntax and conventions.
  • fast-copy: 'fast-copy' is also user-friendly, requiring minimal setup. Its API is intuitive, making it easy to integrate into existing projects for fast cloning needs.

Circular Reference Handling

  • clone: The 'clone' library does not handle circular references well, which can lead to errors or unexpected behavior when cloning objects with such structures.
  • rfdc: 'rfdc' is designed to handle circular references efficiently, providing a reliable way to deep clone objects with such structures without causing errors.
  • immer: 'immer' does not directly clone objects but rather tracks changes to mutable structures. It can handle circular references through its proxy system, making it suitable for complex state management scenarios.
  • lodash.clonedeep: 'lodash.clonedeep' is robust in handling circular references, ensuring that deep cloning works seamlessly even with complex object graphs.
  • fast-copy: 'fast-copy' effectively handles circular references, allowing for safe cloning of complex objects without running into infinite loops or stack overflow errors.

Library Size

  • clone: The 'clone' library is lightweight, making it a good choice for projects where minimizing bundle size is a priority.
  • rfdc: 'rfdc' is designed to be minimal and efficient, keeping its size small while providing high performance for deep cloning tasks.
  • immer: 'immer' is larger than some other cloning libraries due to its additional features for state management, which may be a consideration for projects focused on bundle size.
  • lodash.clonedeep: 'lodash.clonedeep' is part of the larger Lodash library, which can increase bundle size if only cloning functionality is needed. However, it provides a comprehensive set of utilities beyond cloning.
  • fast-copy: 'fast-copy' is also lightweight and optimized for performance, striking a balance between speed and size, suitable for performance-sensitive applications.
How to Choose: clone vs rfdc vs immer vs lodash.clonedeep vs fast-copy
  • clone: Choose 'clone' for its simplicity and ease of use in basic cloning scenarios where performance is not a critical factor.
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.