Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash cloneDeep vs JSON Clone
(version: 0)
Comparing performance of:
Lodash cloneDeep vs Json clone
Created:
7 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.10/lodash.min.js'></script>
Script Preparation code:
var MyObject = { "_id": "5af7e5f9c3a3aa2c8495305e", "index": 0, "guid": "f4a86db0-f457-400e-9280-e4e84a97191c", "isActive": true, "balance": "$1,546.38", "picture": "http://placehold.it/32x32", "age": 35, "eyeColor": "blue", "name": { "first": "Trina", "last": "Bonner" }, "company": "PHOTOBIN", "email": "trina.bonner@photobin.co.uk", "phone": "+1 (907) 417-3108", "address": "132 Rutherford Place, Munjor, Iowa, 9843", "about": "Sit consectetur dolor nostrud dolore culpa cupidatat. Commodo minim qui sint est est excepteur est id commodo est ut sunt qui amet. Aute veniam tempor sunt irure nulla.", "registered": "Tuesday, February 16, 2016 4:23 PM", "latitude": "-21.702025", "longitude": "-41.621726", "tags": [ "consequat", "nulla", "aute", "aute", "aliqua" ], "range": [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ], "friends": [ { "id": 0, "name": "Tommie Noble" }, { "id": 1, "name": "Edna French" }, { "id": 2, "name": "Durham Faulkner" } ], "greeting": "Hello, Trina! You have 8 unread messages.", "favoriteFruit": "strawberry" }; 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:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash cloneDeep
248367.8 Ops/sec
Json clone
437831.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Definition** The benchmark is comparing two approaches to create a deep copy of an object: using Lodash's `cloneDeep` function and using the built-in `JSON.parse(JSON.stringify())` method. **Options Compared** 1. **Lodash cloneDeep**: This approach uses the `cloneDeep` function from the Lodash library, which is a utility library for JavaScript that provides various functions for working with data structures like arrays and objects. 2. **JSON Clone**: This approach uses the built-in `JSON.parse(JSON.stringify())` method to create a deep copy of the object. **Pros and Cons** 1. **Lodash cloneDeep**: * Pros: Generally faster and more efficient than using `JSON.parse(JSON.stringify())`, especially for large objects. * Cons: Requires including an additional library (Lodash) in the project, which may add unnecessary overhead. 2. **JSON Clone**: * Pros: No external dependencies required, as it's a built-in method. * Cons: May be slower and less efficient than `cloneDeep`, especially for large objects. **Library** The Lodash library is used to provide the `cloneDeep` function, which creates a deep copy of an object by recursively cloning all properties of the original object. The `JSON.parse(JSON.stringify())` method, on the other hand, uses JavaScript's built-in serialization and deserialization mechanisms to create a shallow copy of an object. **Special JS Feature** There is no special JavaScript feature or syntax being used in this benchmark. Both approaches are straightforward and don't require any advanced features or syntax. **Other Considerations** When deciding between these two approaches, consider the size and complexity of your objects, as well as the trade-offs between performance, ease of use, and additional dependencies. If you're working with large datasets or complex data structures, `cloneDeep` might be a better choice due to its potential for faster execution times. However, if simplicity and minimalism are more important, using `JSON.parse(JSON.stringify())` could be a viable option. **Alternatives** Other alternatives for creating deep copies of objects include: 1. **`Object.assign()`**: While not as efficient as `cloneDeep`, `Object.assign()` can be used to create a shallow copy of an object. 2. **`lodash.clone()`**: Lodash provides another cloning function called `clone()`, which creates a shallow copy of an object. 3. **Custom implementation**: You could write your own custom function to create a deep copy of an object, using techniques like recursive iteration and property propagation. Ultimately, the choice between these approaches depends on your specific needs and priorities.
Related benchmarks:
Lodash cloneDeep vs JSON Clone (Large Object 2)
Lodash cloneDeep vs JSON Clone Large object
Lodash cloneDeep vs JSON Clone with Array
lodash cloneDeep vs json.stringify
Lodash(v4.17.21) cloneDeep vs JSON Clone Larger object
Comments
Confirm delete:
Do you really want to delete benchmark?