Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash clonedeep vs json.parse(stringify()) vs recursivecopy on larger JSON object
(version: 0)
Comparing performance of:
Lodash CloneDeep vs Json Clone vs recursiveDeepCopy
Created:
2 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 = { "squadName": "Super hero squad", "homeTown": "Metro City", "formed": 2016, "secretBase": "Super tower", "active": true, "members": [ { "name": "Molecule Man", "age": 29, "secretIdentity": "Dan Jukes", "powers": [ "Radiation resistance", "Turning tiny", "Radiation blast" ] }, { "name": "Madame Uppercut", "age": 39, "secretIdentity": "Jane Wilson", "powers": [ "Million tonne punch", "Damage resistance", "Superhuman reflexes" ] }, { "name": "Eternal Flame", "age": 1000000, "secretIdentity": "Unknown", "powers": [ "Immortality", "Heat Immunity", "Inferno", "Teleportation", "Interdimensional travel" ] } ] }; var myCopy = null; function recursiveDeepCopy(o) { var newO, i; if (typeof o !== 'object') { return o; } if (!o) { return o; } if ('[object Array]' === Object.prototype.toString.apply(o)) { newO = []; for (i = 0; i < o.length; i += 1) { newO[i] = recursiveDeepCopy(o[i]); } return newO; } newO = {}; for (i in o) { if (o.hasOwnProperty(i)) { newO[i] = recursiveDeepCopy(o[i]); } } return newO; }
Tests:
Lodash CloneDeep
myCopy = _.cloneDeep(MyObject);
Json Clone
myCopy = JSON.parse(JSON.stringify(MyObject));
recursiveDeepCopy
myCopy = recursiveDeepCopy(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
recursiveDeepCopy
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 what is being tested on the provided JSON. **Benchmark Definition** The benchmark is comparing three different methods for cloning a large JSON object: 1. `lodash.cloneDeep` (Lodash Clone Deep) 2. `JSON.parse(JSON.stringify(MyObject))` (Json Clone) 3. A custom recursive deep copy function named `recursiveDeepCopy` **Options Compared** The options being compared are the performance of each method in cloning a large JSON object. **Pros and Cons of Each Approach** 1. **Lodash Clone Deep** * Pros: Provides a robust and tested implementation for deep copying objects, handles complex data structures like arrays and objects. * Cons: Requires an external library (Lodash), may have additional dependencies or overhead. 2. **Json Clone** * Pros: Simple and lightweight, uses built-in JavaScript methods. * Cons: May not handle all edge cases or complex data structures well, can lead to shallow copying if not used carefully. 3. **recursiveDeepCopy** * Pros: Custom implementation allows for fine-grained control over cloning process, handles arrays and objects recursively. * Cons: Requires manual implementation of deep copy logic, may be more error-prone or harder to maintain. **Library Used** The `lodash.cloneDeep` function is part of the Lodash library, which provides a comprehensive set of utility functions for JavaScript. In this benchmark, Lodash is included in the HTML preparation code using `<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>`. **Special JS Features or Syntax** There are no special JS features or syntax used in this benchmark. **Other Considerations** * The large JSON object being cloned is likely to be a major contributor to the performance differences between these methods. * Other factors like browser and device variations, system resources, and network conditions may also impact the results. * This benchmark focuses on cloning performance; other considerations might include memory usage, garbage collection overhead, or the complexity of the original data structure. **Alternatives** If you're looking for alternatives to these methods, consider: 1. **Built-in `JSON.parse(JSON.stringify())`**: While not ideal, this method can be used with caution and may provide better performance than custom implementations. 2. **Other deep copying libraries**: Libraries like `immer` or ` deepcopier` might offer more efficient or robust solutions for deep cloning objects. 3. **Manual implementation**: Depending on your specific use case, manually implementing a deep copy algorithm can provide fine-grained control and customization. Keep in mind that the choice of method ultimately depends on the specific requirements and constraints of your project.
Related benchmarks:
Lodash 2.2.0 cloneDeep vs JSON Clone w/ large nested object
cloneDeep vs JSON stringify + parse (long arr)
lodash clonedeep vs json.parse(stringify()) vs recursivecopy new big
Lodash cloneDeep vs JSON parse
Lodash cloneDeep vs structuredClone vs recursiveDeepCopy vs JSON clone with a more deep test
Comments
Confirm delete:
Do you really want to delete benchmark?