<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
}],
children: {
description: 'Random description.',
testNumber: 123456789,
testBoolean: true,
testObject: {
testString: 'test string',
testNumber: 12345
},
testArray: [{
myName: 'test name',
myNumber: 123245
}],
children: {
description: 'Random description.',
testNumber: 123456789,
testBoolean: true,
testObject: {
testString: 'test string',
testNumber: 12345
},
testArray: [{
myName: 'test name',
myNumber: 123245
}],
children: {
description: 'Random description.',
testNumber: 123456789,
testBoolean: true,
testObject: {
testString: 'test string',
testNumber: 12345
},
testArray: [{
myName: 'test name',
myNumber: 123245
}],
children: {
description: 'Random description.',
testNumber: 123456789,
testBoolean: true,
testObject: {
testString: 'test string',
testNumber: 12345
},
testArray: [{
myName: 'test name',
myNumber: 123245
}],
children: {
description: 'Random description.',
testNumber: 123456789,
testBoolean: true,
testObject: {
testString: 'test string',
testNumber: 12345
},
testArray: [{
myName: 'test name',
myNumber: 123245
}],
children: {
description: 'Random description.',
testNumber: 123456789,
testBoolean: true,
testObject: {
testString: 'test string',
testNumber: 12345
},
testArray: [{
myName: 'test name',
myNumber: 123245
}],
children: {
description: 'Random description. Random description.Random description.Random description.Random description.Random description.Random description.Random description.Random description.Random description.Random description.Random description.',
testNumber: 123456789,
testBoolean: true,
testObject: {
testString: 'test string',
testNumber: 12345
},
testArray: [{
myName: 'test name',
myNumber: 123245
}],
children: {
description: 'Random description.',
testNumber: 123456789,
testBoolean: true,
testObject: {
testString: 'test string',
testNumber: 12345
},
testArray: [{
myName: 'test name',
myNumber: 123245
}],
children: {
description: 'Random description.',
testNumber: 123456789,
testBoolean: true,
testObject: {
testString: 'test string',
testNumber: 12345
},
testArray: [{
myName: 'test name',
myNumber: 123245
}],
children: {
description: 'Random description.',
testNumber: 123456789,
testBoolean: true,
testObject: {
testString: 'test string',
testNumber: 12345
},
testArray: [{
myName: 'test name',
myNumber: 123245
}],
children: {
description: 'Random Random description.Random description.Random description.Random description.Random description.Random description. description.',
testNumber: 123456789,
testBoolean: true,
testObject: {
testString: 'test string',
testNumber: 12345
},
testArray: [{
myName: 'test name',
myNumber: 123245
}],
children: {
description: 'Random description.',
testNumber: 123456789,
testBoolean: true,
testObject: {
testString: 'test string',
testNumber: 12345
},
testArray: [{
myName: 'test name',
myNumber: 123245
}],
children: {
description: 'RaRandom description.Random description.Random description.Random description.Random description.ndom description.',
testNumber: 123456789,
testBoolean: true,
testObject: {
testString: 'testRandom description.Random description.Random description.Random description. string',
testNumber: 12345
},
testArray: [{
description: 'Random description. Random description.Random description.Random description.Random description.Random description.Random description.Random description.Random description.Random description.Random description.Random description.',
myName: 'test name',
myNumber: 123245
}],
children: {
description: 'Random description.',
testNumber: 123456789,
testBoolean1: new Date(),
testObject: {
testString: 'test string',
testNumber: 12345
},
testArray: [{
myName: 'test name',
myNumber: 123245
}],
children: {
description: 'Random description. Random description.Random description.Random description.Random description.Random description.Random description.Random description.Random description.Random description.Random description.Random description.',
description1: 'Random description.',
testNumber: 123456789,
testBoolean1: true,
testObject: {
testString: 'test string',
testNumber: 12345
},
testArray: [{
description: 'Random description. Random description.Random description.Random description.Random description.Random description.Random description.Random description.Random description.Random description.Random description.Random description.',
testBoolean1: new Date(),
myName: 'test name',
myNumber: 123245,
testBoolean: new Date()
}]
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}];
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);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash cloneDeep | |
Native JSON parse | |
Recursive deep clone | |
structuredClone |
Test name | Executions per second |
---|---|
Lodash cloneDeep | 45000.7 Ops/sec |
Native JSON parse | 57330.4 Ops/sec |
Recursive deep clone | 46832.2 Ops/sec |
structuredClone | 49831.0 Ops/sec |
It appears that we have a JavaScript code snippet with two main parts:
Benchmarking configuration: The first part is an array of objects, where each object represents a benchmark test case. Each test case has the following properties:
Benchmark Definition
: a string that defines the test case.Test Name
: a string that provides a human-readable name for the test case.Code snippet: The second part is a JavaScript code snippet, which includes:
var deepClone = function(obj) {...}
{ "Individual test cases": [...], "Latest benchmark result": [...]" }
Questions:
deepClone
function?Assumptions:
Individual test cases
array contains four test cases, each with a different approach to cloning an object.Latest benchmark result
array contains four benchmark results for each of the test cases.Please feel free to provide more context or clarify any assumptions I may have made.