Test name | Executions per second |
---|---|
Lodash cloneDeep | 452336.8 Ops/sec |
Native JSON parse | 717445.1 Ops/sec |
Recursive deep clone | 364810.7 Ops/sec |
structuredClone | 337578.7 Ops/sec |
<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 = JSON.parse(JSON.stringify(testArray));
testCopy = deepClone(testArray);
testCopy = structuredClone(testArray);