Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash cloneDeep vs freeze vs stringify
(version: 0)
Comparing performance of:
Lodash cloneDeep vs freeze vs Stringify
Created:
5 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, 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 propNames) { let value = object[name]; object[name] = value && typeof value === "object" ? deepFreeze(value) : value; } return Object.freeze(object); } myCopy = deepFreeze(MyObject)
Stringify
myCopy=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
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 Overview** The benchmark compares three different approaches to create a deep copy of an object: 1. Lodash `cloneDeep` 2. Custom implementation using `deepFreeze` (a utility function that freezes objects in place) 3. Stringifying the object using `JSON.stringify` **Lodash `cloneDeep`** * Library: Lodash.js * Purpose: Creates a deep copy of an object, cloning all properties and recursively creating new objects for arrays. Pros: * Easy to use and well-maintained * Handles complex data structures, including arrays and nested objects Cons: * May not be optimized for performance in certain scenarios * Adds external dependency (Lodash.js) **Custom `deepFreeze`** * Purpose: Freezes an object in place, making it immutable. This implementation is used to create a deep copy of the original object. Pros: * Customizable and under-the-hood control over the freezing process * No external dependencies Cons: * More complex to implement and understand * May not handle all edge cases correctly (e.g., frozen objects can still be modified indirectly) **Stringifying using `JSON.stringify`** * Purpose: Converts a JavaScript object to a JSON string, which can be used as a string representation of the original object. Pros: * Fast and lightweight * No external dependencies Cons: * Not suitable for creating deep copies, as it only serializes references to objects * May not handle all data types correctly (e.g., symbols, BigInt) **Other Considerations** * The benchmark uses Mobile Safari 14 on an iPhone with iOS 14.4 as the test device. * The `ExecutionsPerSecond` value represents the number of executions performed per second, which can be used to estimate performance. **Alternatives** If you need to create deep copies of objects in JavaScript, you may also consider using: 1. `Object.assign()` and `Array.prototype.slice()`: These methods can be used to create shallow copies of objects and arrays. 2. `immer` library: A popular alternative to Lodash for immutable data structures. 3. `proptypes` library: Provides a way to create predictable, type-safe data structures. In summary, the benchmark compares three approaches to create deep copies of objects: Lodash `cloneDeep`, custom implementation using `deepFreeze`, and stringifying using `JSON.stringify`. The choice of approach depends on your specific requirements, performance considerations, and personal preference.
Related benchmarks:
Lodash cloneDeep vs freeze
Lodash cloneDeep vs JSON Clone vs freeze and destructure
Lodash cloneDeep vs JSON.parse(JSON.stringify())
lodash cloneDeep vs. JSON.parse(JSON.stringify()) vs. structuredClone
Comments
Confirm delete:
Do you really want to delete benchmark?