Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
cloneDeep vs freeze
(version: 0)
Comparing performance of:
Lodash cloneDeep vs freeze vs stringify/parse
Created:
4 years ago
by:
Registered User
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, jayson: { stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....', parse: 'JSON.parse() method parses a JSON string...' } }; var myCopy = null;
Tests:
Lodash cloneDeep
myCopy = _.cloneDeep(MyObject);
freeze
function deepFreeze(object) { // Retrieve the property names defined on object var propNames = Object.getOwnPropertyNames(object); // Freeze properties before freezing self for (let name of Object.getOwnPropertyNames(object)) { const value = object[name] object[name] = value && typeof value === "object" ? deepFreeze(value) : value } return Object.freeze(object); } myCopy = deepFreeze(MyObject)
stringify/parse
myCopy = JSON.parse(JSON.stringify(MyObject))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Lodash cloneDeep
freeze
stringify/parse
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):
**Benchmark Overview** The provided JSON represents a JavaScript microbenchmarking test created on MeasureThat.net. The benchmark measures the performance of three different methods for creating a deep copy or freezing an object: Lodash's `cloneDeep`, a custom implementation using `Object.freeze`, and the `JSON.stringify` method. **Options Compared** The three options compared in this benchmark are: 1. **Lodash cloneDeep**: This method creates a deep copy of the input object using Lodash's `cloneDeep` function. 2. **Custom freeze implementation**: This method uses a recursive approach to freeze an object, freezing its properties and then its own prototype chain. 3. **JSON.stringify + JSON.parse**: This method creates a deep copy by stringifying the original object and then parsing it back into a new object. **Pros and Cons of Each Approach** 1. **Lodash cloneDeep**: * Pros: Fast, efficient, and widely tested. * Cons: Requires an external library (Lodash) to be included in the test environment. 2. **Custom freeze implementation**: * Pros: Provides a deep understanding of how objects are frozen in JavaScript, can be optimized for specific use cases. * Cons: Can be slower due to the recursive nature of the algorithm and potential performance issues with very large objects. 3. **JSON.stringify + JSON.parse**: * Pros: Simple, widely supported, and efficient for simple data structures. * Cons: May not work correctly with certain types of objects (e.g., those with circular references) or require additional setup to handle such cases. **Library Usage** The test uses Lodash's `cloneDeep` function, which is a popular and well-maintained library for functional programming in JavaScript. The custom freeze implementation uses the built-in `Object.freeze` method to freeze objects. **Special JS Feature/Syntax** None of the benchmarked methods use any special JavaScript features or syntax beyond the standard ECMAScript specification. **Other Alternatives** If you need to create deep copies or freeze objects in your own code, consider the following alternatives: * For simple data structures: Use `JSON.stringify` followed by `JSON.parse`. * For more complex objects: Consider using a library like Lodash's `cloneDeep` or a custom implementation that uses `Object.freeze`. * For very large objects: Optimize the freeze implementation to minimize overhead, as it may be slower than other methods. * For specific use cases (e.g., preserving prototype chain): Use a custom implementation tailored to your requirements. In summary, this benchmark provides a useful comparison of different methods for creating deep copies or freezing objects in JavaScript, highlighting the trade-offs between simplicity, performance, and flexibility.
Related benchmarks:
Lodash cloneDeep vs JSON Clone with huge object
Lodash cloneDeep vs clone vs spread
Lodash cloneDeep vs freeze vs stringify
Lodash cloneDeep vs JSON Clone vs freeze and destructure
Lodash cloneDeep vs JSON Clone vs freeze and get - access a value
Comments
Confirm delete:
Do you really want to delete benchmark?