Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash cloneDeep vs JSON Clone 2
(version: 0)
Comparing performance of:
Lodash cloneDeep vs Json clone
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.11/lodash.min.js'></script>
Script Preparation code:
var MyObject = {}; // Iterate to big object 10 000 times for (var i = 0; i < 10000; i++) { MyObject['prop' + i] = { 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);
Json clone
myCopy = JSON.parse(JSON.stringify(MyObject));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash cloneDeep
Json clone
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 provided JSON and explain what's being tested in this JavaScript microbenchmark. **Benchmark Definition** The benchmark is designed to compare the performance of two different methods for creating a deep copy of an object: 1. `JSON.parse(JSON.stringify(MyObject))` 2. `_ = _.cloneDeep(MyObject)` where `MyObject` is a large object with 10,000 properties, each containing various data types such as numbers, booleans, and nested objects. **Options Compared** The two options being compared are: 1. **JSON.parse(JSON.stringify())**: This method uses the JSON serialization protocol to create a deep copy of the original object. 2. **Lodash's `cloneDeep()` function**: This function is part of the Lodash library and provides a more robust way to create deep copies of objects. **Pros and Cons** **JSON.parse(JSON.stringify())** Pros: * Lightweight: This method is very lightweight, making it suitable for creating small to medium-sized objects. * Simple: The syntax is straightforward and easy to understand. Cons: * Inaccurate: When the original object contains functions or undefined values, `JSON.parse()` will throw errors. Additionally, it may not preserve all properties of the original object. * Slow: For large objects, this method can be slow due to the overhead of serializing and deserializing the data. **Lodash's `cloneDeep()` function** Pros: * Robust: This function is designed to handle complex objects with functions and undefined values. It also preserves more properties than `JSON.parse(JSON.stringify())`. * Fast: For large objects, this method is generally faster due to its optimized implementation. Cons: * Heavyweight: This method requires the Lodash library, which adds size overhead. * Steeper learning curve: The syntax is less familiar to those without experience with functional programming. **Library Used** The Lodash library provides a range of utility functions for working with data structures. In this case, `cloneDeep()` is specifically designed to create deep copies of objects. **Special JS Feature or Syntax** None mentioned in the provided code. However, it's worth noting that some older browsers may not support modern JavaScript features like async/await or modern array methods. If the benchmark were running on an older browser, this might affect performance. **Other Alternatives** For creating deep copies of objects, other options include: 1. `Object.assign()`: This method creates a shallow copy of an object. 2. `Array.prototype.slice()`: This method creates a shallow copy of an array. 3. `immer.js`: A library specifically designed for working with immutable data structures. However, these alternatives may not provide the same level of robustness or performance as Lodash's `cloneDeep()` function.
Related benchmarks:
Lodash 2.2.0 cloneDeep vs JSON Clone w/ large nested object
lodash cloneDeep vs. JSON.parse(JSON.stringify()) vs. fastest-json-copy | On a Small Object
Lodash (4.17.11) cloneDeep vs JSON Clone vs structuredClone
lodash clonedeep vs json.parse(stringify()) vs recursivecopy new big
Lodash cloneDeep vs structuredClone vs recursiveDeepCopy vs JSON clone with a more deep test
Comments
Confirm delete:
Do you really want to delete benchmark?