Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash deepClone vs JSON parse/stringify - complex object
(version: 0)
Comparing performance of:
JSON parse/stringify vs Lodash deepClone
Created:
6 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 obj = { "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 obj2 = null;
Tests:
JSON parse/stringify
obj2 = JSON.parse(JSON.stringify(obj));
Lodash deepClone
obj2 = _.cloneDeep(obj);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
JSON parse/stringify
Lodash deepClone
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 benchmark definition and explain what's being tested. **Benchmark Description** The benchmark measures the performance of two different approaches to create a deep copy of a complex object: `JSON.parse(JSON.stringify(obj))` and `_.cloneDeep(obj)`, where `obj` is a nested JavaScript object. **Options Compared** 1. **JSON.parse(JSON.stringify(obj))**: This approach uses the `JSON.parse()` function to parse a JSON representation of the original object, which effectively creates a shallow copy of it. However, this method does not create a deep copy, as it only recursively copies the top-level properties and arrays. 2. **_.cloneDeep(obj)**: This approach uses the Lodash library's `cloneDeep()` function, which creates a deep copy of the original object by recursively copying all properties and arrays. **Pros and Cons** * **JSON.parse(JSON.stringify(obj))**: + Pros: Simple and widely supported. + Cons: Creates a shallow copy, not suitable for nested objects with cycles (e.g., `obj[0].obj[1] = obj`). * **_.cloneDeep(obj)**: + Pros: Creates a deep copy, handles cycles, and is more accurate than the JSON approach. + Cons: Requires the Lodash library, which may add overhead. **Library Used** The benchmark uses the `lodash.js` library, specifically the `cloneDeep()` function. This library provides a convenient way to create deep copies of objects and arrays in JavaScript. **Special JS Feature/Syntax** There are no special JavaScript features or syntax used in this benchmark that require specific expertise. **Other Considerations** When working with nested objects and cycles, it's essential to use the correct approach to avoid infinite loops. The `JSON.parse(JSON.stringify(obj))` method can lead to unexpected behavior if not used carefully. In addition to these two approaches, there are other ways to create deep copies of objects in JavaScript, such as using the `Object.assign()` method or a recursive function with `JSON.stringify()` and `eval()`. However, these methods may have their own trade-offs and limitations. **Alternatives** Some alternative approaches to creating deep copies of objects include: 1. Using `Object.assign()` with an empty object: `const obj2 = Object.assign({}, obj);` 2. Creating a recursive function: `function cloneDeep(obj) { ... }` (not shown in the benchmark) 3. Using libraries like Immer or DeepClone Keep in mind that these alternatives may have different performance characteristics and requirements compared to the Lodash library used in this benchmark.
Related benchmarks:
Lodash.isEqual vs JSON.stringify Equality Comparison for Shallow Array of Strings.
Lodash.isEqual vs JSON.stringify Equality Comparison for Shallow Array of Strings. Testing 123
Lodash.isEqual vs JSON.stringify Equality Comparison for Shallow Array of Strings. mcki
cloneDeep vs JSON stringify + parse (long arr)
Lodash.isEqual vs JSON.stringify vs join : Equality Comparison for Shallow Array of Strings
Comments
Confirm delete:
Do you really want to delete benchmark?