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
Created:
7 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" }, c: new Date() } }; for(var i = 0; i < 10000; i++) { copyArray.push(_.cloneDeep(copyObject)); }
Tests:
Copy Array x
var y = x([], copyArray); console.log(y);
Copy Array lodash
var y = _.cloneDeep(copyArray) console.log(y);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Copy Array x
Copy Array lodash
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 compares two methods for creating deep copies of complex JavaScript objects: **Method 1: `x` function (Custom Implementation)** * This method implements a recursive function `x` that iterates through the properties of an object and recursively calls itself to copy nested objects. It creates a new instance of each nested object's constructor to ensure a true deep copy. * **Pros:** Potentially more performant if optimized for specific use cases. Offers direct control over the copying logic. * **Cons:** Requires manual implementation and testing, potentially more complex to maintain. **Method 2: `_.cloneDeep` (Lodash Library)** * This method utilizes the `_.cloneDeep` function from the Lodash library. Lodash is a popular JavaScript utility library that provides many helpful functions for working with data structures. * **Pros:** Concise and easy to use, already tested and optimized by the Lodash team. Leverage the existing functionality of a well-established library. * **Cons:** Adds a dependency on the Lodash library, which might increase bundle size and potential conflicts if not managed carefully. **Benchmark Results:** The benchmark results indicate that the custom `x` function is generally faster than using `_.cloneDeep`. However, keep in mind that these results can vary depending on factors like: * **Object Complexity:** The complexity of the object being copied (number of nested levels) significantly influences performance. * **Implementation Details:** Even slight optimizations within the `x` function could lead to substantial performance gains. **Alternatives:** * Built-in JavaScript methods: Consider using `JSON.parse(JSON.stringify(object))` for simpler objects. However, this approach doesn't handle circular references correctly. * Other Libraries: Explore alternatives to Lodash like Ramda or Immer, which might offer different performance characteristics or specialized features. Let me know if you have any further questions!
Related benchmarks:
Lodash cloneDeep vs for loop vs JSON parse vs recursive clone deep vs recursive reduce clone deep
JavaScript spread operator vs cloneDeep
is lodash cloneDeep the BEST object deep cloner ? what about native structuredClone function ?
COPY TEST 0705
Lodash clone deep object array vs string array
Comments
Confirm delete:
Do you really want to delete benchmark?