<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var obj = {a: "hello", c: "test", num: 3, arr: [1, 2, 3, 4], anotherObj: {a: 35, str: "tester"}};
var deepClone = function(obj) {
var out = null;
if (Array.isArray(obj)) {
out = [];
for (var index = 0; index < obj.length; ++index) {
var subArray = obj[index];
out.push((typeof subArray === 'object') ? deepClone(subArray) : subArray);
}
} else {
out = {};
for (var key in obj) {
var subObject = obj[key];
out[key] = (typeof subObject === 'object') ? deepClone(subObject) : subObject;
}
}
return out;
};
function cloneArray(value) {
var newArray;
if (value && value.length) {
newArray = [];
for (var i = 0; i < value.length; i++) {
if (typeof value[i] === 'object') {
newArray[i] = Array.isArray(value[i]) ? cloneArray(value[i]) : cloneObject(value[i]);
} else {
newArray[i] = value[i];
}
}
} else if (value === null) {
newArray = null;
}
return newArray;
}
function cloneObject(value) {
var newObject;
if (value && typeof value === 'object' && Object.keys(value).length > 0) {
newObject = {};
for (var key in value) {
if (typeof value[key] === 'object') {
newObject[key] = Array.isArray(value) ? cloneArray(value[key]) : cloneObject(value[key]);
} else {
newObject[key] = value[key];
}
}
} else if (value && typeof value === 'object' && Object.keys(value).length === 0) {
newObject = {};
} else if (value === null) {
newObject = null;
}
return newObject;
}
function reduceDeepClone(obj) {
return Object.keys(obj).reduce((v, d) => Object.assign(v, {
[d]: (obj[d].constructor === Object) ? reduceDeepClone(obj[d]) : obj[d]
}), {});
}
testCopy = _.cloneDeep(obj);
testCopy = JSON.parse(JSON.stringify(obj));
testCopy = cloneObject(obj);
testCopy = deepClone(obj);
testCopy = reduceDeepClone(obj);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash cloneDeep | |
JSON parse | |
For loop | |
Recursive clone deep | |
Recursive reduce clone deep |
Test name | Executions per second |
---|---|
Lodash cloneDeep | 401372.5 Ops/sec |
JSON parse | 556042.0 Ops/sec |
For loop | 645912.9 Ops/sec |
Recursive clone deep | 1949093.5 Ops/sec |
Recursive reduce clone deep | 420276.2 Ops/sec |
Benchmark Explanation
The provided JSON represents a JavaScript microbenchmarking test case named "Lodash cloneDeep vs for loop vs JSON parse vs recursive clone deep vs recursive reduce clone deep". The benchmark compares the execution performance of five different methods to create a deep copy (clone) of an object:
Object.keys()
and reduce()
to create a deep clone of an object.Options Compared
The benchmark compares the performance of these five methods across different browsers (Mobile Safari 17) on an iPhone with iOS 17.6.1 operating system.
Pros and Cons of Each Approach
Object.keys()
and reduce()
, which are efficient built-in methods.Benchmark Results
The benchmark results show the number of executions per second (ExecutionsPerSecond) for each method across different browsers on an iPhone with iOS 17.6.1 operating system. The top performer is Lodash.cloneDeep, followed by the recursive clone deep and recursive reduce clone deep implementations. Note that the order may vary depending on the specific browser and device.
In summary, this benchmark highlights the performance differences between various methods for creating a deep copy of an object in JavaScript.