<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var testArray = [{
description: 'Random description.',
testNumber: 123456789,
testBoolean: true,
testObject: {
testString: 'test string',
testNumber: 12345
},
testArray: [{
myName: 'test name',
myNumber: 123245
}]
},{
description: 'Random description.',
testNumber: 123456789,
testBoolean: true,
testObject: {
testString: 'test string',
testNumber: 12345
},
testArray: [{
myName: 'test name',
myNumber: 123245
}]
}];
var testCopy = null;
var deepClone = function(obj) {
var out;
if (Array.isArray(obj)) {
out = [];
for (var index = 0; index < obj.length; ++index) {
let subArray = obj[index];
out.push((subArray === null) ? subArray : (subArray instanceof Date) ? new Date(subArray.valueOf()) : (typeof subArray === 'object') ? deepClone(subArray) : subArray);
}
} else {
out = {};
for (var key in obj) {
var subObject = obj[key];
out[key] = subObject === null ? subObject : subObject instanceof Date ? new Date(subObject.valueOf()) : (typeof subObject === 'object') ? deepClone(subObject) : subObject;
}
}
return out;
};
testCopy = _.cloneDeep(testArray);
testCopy = testArray.map(arr => testArray.slice());
testCopy = JSON.parse(JSON.stringify(testArray));
testCopy = testArray.map(arr => testArray.slice(0));
testCopy = testArray.map(arr => arr);
testCopy = deepClone(testArray);
testCopy = testArray.map(arr => deepClone(arr));
testCopy = Object.assign([testArray]);
testCopy = structuredClone(testArray);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash cloneDeep | |
Native map deep slice(turns objects into arrays) | |
Native JSON parse | |
Native map deep slice at 0(turns objects into arrays) | |
Native map(shallow clone) | |
Recursive deep clone | |
Map deep clone | |
Object.assign shallow clone | |
structuredClone |
Test name | Executions per second |
---|---|
Lodash cloneDeep | 358790.6 Ops/sec |
Native map deep slice(turns objects into arrays) | 8343281.5 Ops/sec |
Native JSON parse | 542964.2 Ops/sec |
Native map deep slice at 0(turns objects into arrays) | 8317737.5 Ops/sec |
Native map(shallow clone) | 9308361.0 Ops/sec |
Recursive deep clone | 2167404.0 Ops/sec |
Map deep clone | 2272635.2 Ops/sec |
Object.assign shallow clone | 8479168.0 Ops/sec |
structuredClone | 236132.2 Ops/sec |
Let's break down the benchmark and explain what is being tested, compared, and their pros and cons.
Benchmark Overview
The benchmark tests the performance of different methods for copying or cloning arrays in JavaScript. The test cases compare various approaches, including native methods, libraries like Lodash, and a proposed structure clone method.
Tested Methods and Libraries
Here's a summary of the tested methods:
map()
with slice()
to create a shallow copy of an array.slice(0)
instead of slice()
.Object.assign()
to create a shallow copy of an array.cloneDeep()
function to create a deep copy of an array.JSON.parse()
method (which does nothing) as a comparison point for performance.Comparison and Pros/Cons
Here's a brief summary of each method:
Performance Comparison
The benchmark results show the performance of each method across different browsers and devices. The top performer is the native map deep slice
method, followed closely by the Lodash cloneDeep()
function. The proposed structure clone method performs well as well, considering its custom implementation.
In general, the results suggest that:
The benchmark provides a useful insight into the performance characteristics of various array cloning methods in JavaScript.