<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 = clone(MyObject, false);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash cloneDeep | |
Json clone | |
Ramda clone | |
Pvorb Clone |
Test name | Executions per second |
---|---|
Lodash cloneDeep | 1240338.6 Ops/sec |
Json clone | 1064943.1 Ops/sec |
Ramda clone | 1106685.6 Ops/sec |
Pvorb Clone | 1347483.2 Ops/sec |
Let's break down the provided JSON and explain what is tested, the options compared, their pros and cons, and other considerations.
Benchmark Definition: The benchmark defines four test cases for cloning a JavaScript object. The tests compare the performance of three different libraries: Lodash, Ramda, and Pvorb.
Test Cases:
cloneDeep
function from Lodash to create a deep copy of the input object (MyObject
). cloneDeep
is designed to recursively clone all properties of an object, including nested objects and arrays.JSON.parse(JSON.stringify(MyObject))
approach to clone the input object. This method is a simple way to create a shallow copy of an object by converting it to a JSON string and then parsing it back into a JavaScript object.R.clone
function from Ramda to create a deep copy of the input object (MyObject
). Like Lodash, Ramda's clone
function recursively clones all properties of an object.clone(MyObject, false)
function from Pvorb to create a deep copy of the input object (MyObject
). The false
flag tells Pvorb not to use a cached version of the cloned object.Options Compared:
The benchmark compares the performance of these four approaches:
Other Considerations:
Libraries:
Special JS Features/Syntax:
The benchmark does not explicitly use any special JavaScript features or syntax beyond standard ES5/ES6 functionality.
I hope this explanation helps software engineers understand what is being tested and compared in this benchmark!