Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash clonedeep vs json.parse(stringify()) vs recursivecopy _ tieplv
(version: 0)
Comparing performance of:
Lodash CloneDeep vs Json Clone vs recursiveDeepCopy
Created:
3 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 = { "problems": [{ "Diabetes":[{ "medications":[{ "medicationsClasses":[{ "className":[{ "associatedDrug":[{ "name":"asprin", "dose":"", "strength":"500 mg" }], "associatedDrug#2":[{ "name":"somethingElse", "dose":"", "strength":"500 mg" }] }], "className2":[{ "associatedDrug":[{ "name":"asprin", "dose":"", "strength":"500 mg" }], "associatedDrug#2":[{ "name":"somethingElse", "dose":"", "strength":"500 mg" }] }] }] }], "labs":[{ "missing_field": "missing_value" }] }], "Asthma":[{}] }]}; 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 dive into the world of JavaScript microbenchmarks and explore what's being tested in this specific benchmark. **Benchmark Definition** The benchmark is designed to compare three different approaches for creating a deep copy of an object: Lodash's `cloneDeep`, JSON parsing with `JSON.parse(JSON.stringify())`, and a custom recursive function called `recursiveDeepCopy`. **Options Compared** Here are the options being compared: 1. **Lodash CloneDeep**: Uses the `lodash` library to create a deep copy of an object. 2. **Json Clone**: Uses the `JSON.parse()` method with `JSON.stringify()` to create a deep copy of an object by serializing and deserializing it. 3. **Recursive Copy (recursiveDeepCopy)**: A custom function written in JavaScript that recursively traverses the object graph to create a deep copy. **Pros and Cons** Here are some pros and cons for each approach: 1. **Lodash CloneDeep**: * Pros: + Fast and efficient, as it leverages optimized C++ code under the hood. + Supports complex data structures like arrays and objects with nested properties. * Cons: + Requires an additional library dependency (`lodash`). + May not be suitable for very large datasets due to potential memory constraints. 2. **Json Clone**: * Pros: + Lightweight and easy to implement, as it relies on standard JavaScript methods. + Works well for simple data structures like objects with a few nested properties. * Cons: + Can be slower than `cloneDeep` due to the overhead of serializing and deserializing data. + May not work correctly for complex data structures or circular references. 3. **Recursive Copy (recursiveDeepCopy)**: * Pros: + Completely self-contained, as it doesn't rely on any external libraries. + Supports arbitrary data structures and can handle circular references. * Cons: + Can be slower than `cloneDeep` due to the overhead of recursive function calls. + May require more code and maintenance efforts. **Library: Lodash** Lodash is a popular JavaScript library that provides a wide range of utility functions, including cloning and manipulation of objects. In this benchmark, Lodash's `cloneDeep` method is used to create a deep copy of the input object. **Special JS Feature/Syntax** This benchmark does not explicitly test any special JavaScript features or syntax, such as async/await, Promises, or modern ES6+ features like classes or modules. However, it's worth noting that the `JSON.parse()` method is an older JavaScript feature that has been around since ECMAScript 5 (2015). **Other Alternatives** If you're looking for alternative deep copy libraries or methods, here are a few options: * **Immutable.js**: A JavaScript library that provides immutable data structures and functions for creating copies of existing objects. * **DeepCopy.js**: A lightweight JavaScript library specifically designed for creating deep copies of objects. * **Array.prototype.slice() + JSON.parse(JSON.stringify())**: Another approach to creating a deep copy by using the `slice()` method on arrays and combining it with `JSON.parse()`. Keep in mind that these alternatives may have different performance characteristics, trade-offs, or use cases compared to Lodash's `cloneDeep`.
Related benchmarks:
Lodash 2.2.0 cloneDeep vs JSON Clone w/ large nested object
Lodash cloneDeep vs JSON Clone with Array
cloneDeep vs JSON stringify + parse (long arr)
lodash cloneDeep vs json.stringify
Lodash cloneDeep vs JSON parse
Comments
Confirm delete:
Do you really want to delete benchmark?