<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"
}
};
function altClone(obj) {
if (obj) {
let clone;
const isNested = (item) => typeof item === 'object';
if (Array.isArray(obj)) {
clone = [];
obj.forEach((value) => {
clone.push(isNested(value) ? deepClone(value) : value);
});
} else {
clone = {};
Object.keys(obj).forEach((key) => {
const value = obj[key];
clone[key] = isNested(value) ? deepClone(value) : value;
});
}
return clone;
}
return obj;
};
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 structured(value) {
return window.structuredClone(value)
}
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);
testCopy = altClone(obj);
testCopy = structured(obj);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash cloneDeep | |
JSON parse | |
For loop | |
Recursive clone deep | |
Recursive reduce clone deep | |
altClone | |
structured |
Test name | Executions per second |
---|---|
Lodash cloneDeep | 231973.1 Ops/sec |
JSON parse | 228410.0 Ops/sec |
For loop | 238612.4 Ops/sec |
Recursive clone deep | 464329.9 Ops/sec |
Recursive reduce clone deep | 133374.6 Ops/sec |
altClone | 361491.7 Ops/sec |
structured | 108949.8 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Definition
The benchmark compares different methods for creating deep clones of an object. The object obj
is defined in the HTML preparation code, which includes a reference to Lodash.js.
Methods being compared
_cloneDeep
function from Lodash to create a deep clone of the object.JSON.parse(JSON.stringify(obj))
to create a deep clone of the object by serializing and deserializing the object.deepClone(obj)
to create a deep clone of the object, as defined in the benchmark definition.reduceDeepClone(obj)
to create a deep clone of the object, which is similar but slightly different from the previous one (not shown in the code snippet).altClone(obj)
to create a deep clone of the object, as defined in the benchmark definition.structured(obj)
to create a deep clone of the object, which is not clearly explained but seems to be another proprietary method.What's being tested
The benchmark measures the execution time of each method, typically measured in executions per second (ExecutionsPerSecond). The goal is likely to determine which method is fastest, most efficient, or produces the best results.
Now, let's discuss some pros and cons of each method:
Overall, this benchmark seems to be focused on comparing different deep cloning methods for objects, with an emphasis on execution time and performance.