Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
test clone deep
(version: 0)
Comparing performance of:
Copy Array x vs Copy Array lodash vs Copy Array json
Created:
8 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:
function x(clone, obj) { for(var i in obj) clone[i] = (typeof obj[i] == "object" && obj[i] != null) ? x(obj[i].constructor(), obj[i]) : obj[i]; return clone; } var copyArray = []; var copyObject = { id: 1, name: "Hello", x: true, i: { y: false, a: { b: { nested: true, last: "world" }, foo: "bar" } } }; for(var i = 0; i < 10000; i++) { copyArray.push(_.cloneDeep(copyObject)); }
Tests:
Copy Array x
var y = x([], copyArray);
Copy Array lodash
var y = _.cloneDeep(copyArray)
Copy Array json
var y = JSON.parse(JSON.stringify(copyArray))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Copy Array x
Copy Array lodash
Copy Array json
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
gemma2:9b
, generated one year ago):
This benchmark tests the performance of three different approaches to creating a deep copy of a complex JavaScript object: **1. `x()` function:** This custom recursive function iterates through each property of the original object and recursively calls itself for nested objects, effectively creating a new instance with copies of all data. * **Pros:** Can be potentially faster than using libraries if heavily optimized. * **Cons:** Requires manual implementation and debugging, may not handle complex edge cases as efficiently as libraries. **2. `_.cloneDeep()` from Lodash:** This function from the Lodash library uses its own optimized algorithm to create a deep copy of an object, handling various data types and nested structures. * **Pros:** Well-tested and optimized by experienced developers, handles a wide range of data types and scenarios. * **Cons:** Adds external dependency on the Lodash library. **3. `JSON.parse(JSON.stringify(copyArray))`:** This approach converts the object to a JSON string using `JSON.stringify()`, then parses it back into a new object using `JSON.parse()`. * **Pros:** Built-in JavaScript functionality, no additional libraries required. * **Cons:** Can be slower than other approaches due to the stringification and parsing process, may not handle all complex data types efficiently (e.g., functions, custom objects). **Other Alternatives:** * Libraries like `deep-copy` or `util` (Node.js) offer alternative deep copying implementations. * Modern JavaScript features like spread syntax (`...`) can be used for shallow copies of arrays and objects, but not for deep copies. The benchmark results on MeasureThat.net show that the custom recursive function (`x()`) performs best in this specific scenario. This could be due to its targeted implementation for the particular data structure being copied. However, it's important to consider the trade-offs between performance and maintainability when choosing a deep copying approach.
Related benchmarks:
Lodash cloneDeep vs for loop vs JSON parse vs recursive clone deep vs recursive reduce clone deep
ES6 vs Lodash object copying
Comparing deep cloning methods (array of objects): Lodash <> Custom clone func <> JSON.parse <> structuredClone
is lodash cloneDeep the BEST object deep cloner ? what about native structuredClone function ?
Lodash clone deep object array vs string array
Comments
Confirm delete:
Do you really want to delete benchmark?