Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
new Recursive Clone VS other Cloning Methods
(version: 0)
compare object deep copy/clone methods
Comparing performance of:
Lodash cloneDeep vs JSON parse vs For loop vs Recursive clone deep vs Recursive reduce clone deep vs altClone vs structured
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
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] }), {}); }
Tests:
Lodash cloneDeep
testCopy = _.cloneDeep(obj);
JSON parse
testCopy = JSON.parse(JSON.stringify(obj));
For loop
testCopy = cloneObject(obj);
Recursive clone deep
testCopy = deepClone(obj);
Recursive reduce clone deep
testCopy = reduceDeepClone(obj);
altClone
testCopy = altClone(obj);
structured
testCopy = structured(obj);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (7)
Previous results
Fork
Test case name
Result
Lodash cloneDeep
JSON parse
For loop
Recursive clone deep
Recursive reduce clone deep
altClone
structured
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
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** 1. **Lodash cloneDeep**: Uses the `_cloneDeep` function from Lodash to create a deep clone of the object. 2. **JSON parse**: Uses `JSON.parse(JSON.stringify(obj))` to create a deep clone of the object by serializing and deserializing the object. 3. **For loop**: Creates a deep clone of the object using a custom implementation with a for loop (not shown in the code snippet). 4. **Recursive clone deep**: Uses a recursive function `deepClone(obj)` to create a deep clone of the object, as defined in the benchmark definition. 5. **Recursive reduce clone deep**: Uses another recursive function `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). 6. **altClone**: Uses a custom implementation `altClone(obj)` to create a deep clone of the object, as defined in the benchmark definition. 7. **structured**: Uses a custom implementation `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: * **Lodash cloneDeep**: Easy to use, well-tested, and widely adopted. However, it may not be the fastest option. * **JSON parse**: Simple and straightforward but can lead to performance issues due to overhead from serialization and deserialization. * **For loop**: Custom implementation, so its performance depends on how well it's optimized. May not be as efficient as other methods. * **Recursive clone deep** and **Recursive reduce clone deep**: These methods are likely implemented by the same author or have similar logic. Recursive functions can lead to stack overflow errors if not implemented carefully. * **altClone** and **structured**: These proprietary methods may offer performance advantages but require more expertise to implement and maintain. Overall, this benchmark seems to be focused on comparing different deep cloning methods for objects, with an emphasis on execution time and performance.
Related benchmarks:
Object Deep Copy Test3
Lodash cloneDeep vs for loop vs JSON parse vs recursive clone deep vs recursive reduce clone deep
Comparing deep cloning methods (array of objects): Lodash <> Custom clone func <> JSON.parse <> structuredClone
Object.assign vs. JSON String/Parse vs deepclone
Comments
Confirm delete:
Do you really want to delete benchmark?