<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/clone/2.1.2/clone.min.js"></script>
var MyObject = {
description: 'Creates a deep copy of source, which should be an object or an array.',
myNumber: 123456789,
myBoolean: true,
jayson: {
stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....',
parse: 'JSON.parse() method parses a JSON string...'
}
};
var myCopy = null;
myCopy = _.cloneDeep(MyObject);
myCopy = JSON.parse(JSON.stringify(MyObject));
myCopy = R.clone(MyObject);
myCopy = {MyObject};
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash cloneDeep | |
Json clone | |
Ramda clone | |
JSON Spread |
Test name | Executions per second |
---|---|
Lodash cloneDeep | 708184.6 Ops/sec |
Json clone | 318205.8 Ops/sec |
Ramda clone | 1025021.5 Ops/sec |
JSON Spread | 5200920.5 Ops/sec |
Let's break down the benchmark and its options.
Benchmark Overview
The benchmark compares the performance of four different methods to create a deep copy of an object: Lodash cloneDeep
, JSON Clone, Ramda clone
, and JSON Spread.
Options Compared
cloneDeep
: A utility function from the Lodash library that creates a deep copy of an object.clone
: A higher-order function from the Ramda library that creates a deep copy of an object.Pros and Cons
cloneDeep
:clone
:Library Descriptions
Special JavaScript Features/Syntax
None mentioned in this benchmark.
Other Considerations
Alternatives
When choosing a method for deep copying objects, consider factors like performance requirements, complexity of data structures, and compatibility with your project's dependencies.