<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js'></script>
<script src='https://cdn.jsdelivr.net/npm/jsondiffpatch@0.4.1/dist/jsondiffpatch.umd.min.js'></script>
<script src='https://cdn.jsdelivr.net/npm/rfdc@1.3.0'></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;
var jsondiffpatchClone = jsondiffpatch.clone;
var rfdcClone = rfdc();
///////////////////////////////////////////////////////////////////
function recursiveDeepCopy(o) {
var newO,
i;
if (typeof o !== 'object') {
return o;
}
if (!o) {
return o;
}
if ('[object Array]' === Object.prototype.toString.apply(o)) {
newO = [];
for (i = 0; i < o.length; i += 1) {
newO[i] = recursiveDeepCopy(o[i]);
}
return newO;
}
newO = {};
for (i in o) {
if (o.hasOwnProperty(i)) {
newO[i] = recursiveDeepCopy(o[i]);
}
}
return newO;
}
//////////////////////////////////////////////////////////////////
// Use https://www.npmjs.com/package/fastest-json-copy
// Instead of the code below! It is the same thing, just minified.
var isArray=Array.isArray;function fastestJsonCopy(r){if(!r)return r;if(isArray(r)){for(var e=[],o=r.length,t=0;t<o;t++)e.push(fastestJsonCopy(r[t]));return e}if("object"==typeof r){var y=Object.keys(r),a=(o=y.length,{});for(t=0;t<o;t++){var c=y[t];a[c]=fastestJsonCopy(r[c])}return a}return r}
myCopy = _.cloneDeep(MyObject);
myCopy = JSON.parse(JSON.stringify(MyObject));
myCopy = recursiveDeepCopy(MyObject);
myCopy = fastestJsonCopy(MyObject);
myCopy = jsondiffpatchClone(MyObject);
myCopy = rfdcClone(MyObject);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash cloneDeep | |
JSON.parse(JSON.stringify()) | |
recursiveDeepCopy | |
fastest-json-copy npm package | |
jsondiffpatch.clone() | |
rfdc |
Test name | Executions per second |
---|---|
lodash cloneDeep | 1883537.2 Ops/sec |
JSON.parse(JSON.stringify()) | 1575473.8 Ops/sec |
recursiveDeepCopy | 5888464.0 Ops/sec |
fastest-json-copy npm package | 5188675.0 Ops/sec |
jsondiffpatch.clone() | 5843849.5 Ops/sec |
rfdc | 6499450.0 Ops/sec |
Let's dive into the explanation of the provided benchmark.
Benchmark Overview
The benchmark measures the performance of different methods for creating a deep copy of an object in JavaScript. The test case uses a small object with various data types (number, boolean, and nested object) to simulate real-world scenarios.
Methods Compared
lodash.cloneDeep()
: A popular library function that creates a deep clone of an object.JSON.parse(JSON.stringify())
: A built-in JavaScript method that converts a value to a JSON string and then parses it back into an object, effectively creating a deep copy.fastest-json-copy
(npm package): A lightweight library function specifically designed for fast JSON copying.jsondiffpatch.clone()
: A method from the jsondiffpatch
library that creates a deep clone of an object based on the differences between the original and cloned objects.rfdc()
(recursive deep copy): A custom implementation of a recursive deep copy function.Options Compared
The options compared are:
Pros and Cons of Each Approach
lodash.cloneDeep()
:JSON.parse(JSON.stringify())
:fastest-json-copy
(npm package):jsondiffpatch.clone()
:rfdc()
:Benchmark Results
The provided benchmark results show the execution rate (executions per second) for each method, which indicates the relative performance of each approach. The top performers are:
fastest-json-copy
(npm package)rfdc()
jsondiffpatch.clone()