Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash cloneDeep vs JSON Clone vs freeze and destructure -w/ dates
(version: 0)
Comparing performance of:
Lodash cloneDeep vs Json clone vs freeze and destructure
Created:
5 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...' }, d1: new Date(), date2: new Date().setYear(1980) }; var myCopy = null;
Tests:
Lodash cloneDeep
myCopy = _.cloneDeep(MyObject);
Json clone
myCopy = JSON.parse(JSON.stringify(MyObject));
freeze and destructure
Object.freeze(MyObject); myCopy = { ...MyObject };
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Lodash cloneDeep
Json clone
freeze and destructure
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 three different methods for creating a deep copy of a JavaScript object: * **Lodash `cloneDeep`:** This method utilizes the Lodash library, specifically its `cloneDeep` function. Lodash is a popular library that provides utility functions for working with JavaScript objects and arrays. * **Pros:** Generally efficient and reliable for creating deep copies of complex objects. Handles nested objects and arrays effectively. * **Cons:** Requires including the Lodash library, which adds some overhead to your project. * **JSON Clone (`JSON.stringify`/`JSON.parse`)**: This method leverages the built-in `JSON` object in JavaScript. It serializes the object into a JSON string and then parses it back into a new object. * **Pros:** Native to JavaScript, so no external libraries are needed. Can handle simple and complex objects. * **Cons:** Can be slower than other methods for large or deeply nested objects. Doesn't preserve all data types (like functions) accurately. * **`Object.freeze`/Destructuring:** This method utilizes the `Object.freeze()` method to prevent modifications to the original object and then uses destructuring assignment (`{ ...MyObject }`) to create a new copy. * **Pros:** Potentially fast, as it leverages built-in JavaScript features. Doesn't require external libraries. Can be more efficient for simple objects. * **Cons:** Might not handle all complex object structures or edge cases as effectively as `cloneDeep`. **Benchmarking Context:** The benchmark measures the execution speed of each method in creating a deep copy of the `MyObject` structure provided. The results indicate that the "freeze and destructure" approach is the fastest in this specific scenario, followed by Lodash's `cloneDeep`, and lastly, the JSON cloning method. **Other Alternatives:** * **Manual Recursion:** You could implement your own recursive function to copy objects manually. This offers fine-grained control but can be more complex to write and debug. The best approach depends on factors like: * The complexity of the object structure. * Performance requirements. * Whether you need a deep copy or a shallow copy.
Related benchmarks:
Lodash clone vs cloneDeep
Lodash cloneDeep vs JSON Clone with Array
Lodash cloneDeep vs JSON Clone vs freeze and destructure vs vanilla cloneDeep
Lodash cloneDeep vs JSON Clone vs freeze and get - access a value
Comments
Confirm delete:
Do you really want to delete benchmark?