Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash cloneDeep vs freeze vs monokee deepClone
(version: 0)
Comparing performance of:
Lodash cloneDeep vs custom deepFreeze() vs custom deepClone()
Created:
4 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 MyObject = { description: 'Creates a deep copy of source, which should be an object or an array.', myNumber: 123456789, myBoolean: true, array: [1,2,3,4,5,6,7,8,9,0], nestedObject: { a: [{x: 1}, {x: 1}, {x: 1}], b: [{x: 1}, {x: 1}, {x: 1}], c: [{x: 1}, {x: 1}, {x: 1}] }, jayson: { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses a JSON string...' } }; var myCopy = null; function deepFreeze(x) { if (!x || typeof x !== 'object') { return x; } if (Array.isArray(x)) { const y = []; for (let i = 0; i < x.length; i++) { y.push(deepFreeze(x[i])); } return Object.freeze(y); } const y = {}; for (const key in x) { if (x.hasOwnProperty(key)) { y[key] = deepFreeze(x[key]); } } return Object.freeze(y); } function deepClone(x) { if (!x || typeof x !== 'object') { return x; } if (Array.isArray(x)) { const y = []; for (let i = 0; i < x.length; i++) { y.push(deepClone(x[i])); } return y; } const y = {}; for (const key in x) { if (x.hasOwnProperty(key)) { y[key] = deepClone(x[key]); } } return y; }
Tests:
Lodash cloneDeep
myCopy = _.cloneDeep(MyObject);
custom deepFreeze()
myCopy = deepFreeze(MyObject)
custom deepClone()
myCopy = deepClone(MyObject);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Lodash cloneDeep
custom deepFreeze()
custom deepClone()
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 dive into the world of microbenchmarks. The provided JSON represents a benchmark test created using MeasureThat.net, which compares the performance of three different approaches for creating deep copies of objects: Lodash's `cloneDeep`, custom implementation `deepFreeze`, and another custom implementation `deepClone`. **Tested Options:** 1. **Lodash cloneDeep**: This is a built-in function in Lodash that creates a deep copy of an object or array. 2. **Custom deepFreeze**: This is a custom implementation of creating a frozen (immutability) deep copy of an object. 3. **Custom deepClone**: This is another custom implementation for creating a deep copy of an object. **Pros and Cons:** * **Lodash cloneDeep**: * Pros: Simple to use, well-tested, and optimized for performance. * Cons: Additional dependency on Lodash library, may not be suitable for all edge cases (e.g., circular references). * **Custom deepFreeze**: * Pros: Provides an option for creating a frozen copy of objects, which can be useful in certain scenarios. * Cons: Custom implementation, which might incur additional overhead due to the creation and freezing process. * **Custom deepClone**: * Pros: Another custom implementation that offers flexibility, but may require more development effort compared to Lodash cloneDeep. * Cons: Might introduce performance overhead or inconsistencies in behavior for certain edge cases. **Other Considerations:** * The custom implementations (`deepFreeze` and `deepClone`) use the `Object.freeze()` method, which can be beneficial for preventing unintended changes to objects but might incur additional computation time due to its creation process. * Lodash cloneDeep uses a more efficient algorithm that minimizes overhead during deep copying. **Library Usage:** The `lodash` library is used in this benchmark. It provides various utility functions, including the `cloneDeep()` method, which creates a deep copy of an object or array. The implementation uses a recursive approach to traverse the original object and create new copies for each property. **Special JS Feature/Syntax:** There are no specific JavaScript features or syntaxes being tested in this benchmark, apart from using the built-in `Object.freeze()` method and Lodash's `cloneDeep()` function. Now that we've covered the basics of this benchmark test, let's look at other alternatives: * **Other deep copy libraries**: In addition to Lodash cloneDeep, there are other deep copy libraries available for JavaScript, such as `lodash-deep-extend`, `fast-deep-copy`, or custom implementations like the ones provided in this benchmark. * **Implementation-specific optimizations**: Depending on the specific requirements and constraints of your project, you might want to consider implementing your own optimization techniques, such as caching, memoization, or parallel processing, to further improve deep copy performance. In summary, the MeasureThat.net benchmark provides a convenient way to compare the performance of different approaches for creating deep copies of objects in JavaScript. By understanding the strengths and weaknesses of each approach, you can make informed decisions about which method best suits your specific needs.
Related benchmarks:
Lodash 2.2.0 cloneDeep vs JSON Clone w/ large nested object
Lodash cloneDeep vs JSON Clone vs Bitfish Simple Clone
Lodash cloneDeep vs JSON Clone vs fastDeepClone
Lodash cloneDeep vs deepFreeze vs deepClone vs JSON.parse(JSON.stringify())
Comments
Confirm delete:
Do you really want to delete benchmark?