Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Deep copy benchmark 2
(version: 2)
Comparing performance of:
lodash clone deep vs JSON parse stringify vs Recursive deep copy
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> 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; } </script>
Script Preparation code:
var MyObject = {"Id":"webtracker-sessions","Name":"Sessions","DataSources":[{"Id":1,"EndPointUrl":"https://d2insightsrestapi4620170917123759.azurewebsites.net/api/v2/timechart/session/visits","EndPointRequestType":"post","Name":"Sessions"}],"ChartType":"XTime","HasAnnotations":true,"DefaultTitle":"Sessions","Visualizations":[{"DataSourceId":1,"Type":"timebar","Events":[],"Index":1}],"Configurables":[{"Grouping":"Date & Time","Configurables":[{"Name":"Date Range","Type":"datetime-range","RequestParameters":["StartDate","EndDate"],"Variable":"dates[]"},{"Name":"Atomicity","Type":"time-atomicity","RequestParameters":"Atomicity","Variable":"atomicity"},{"Name":"Timeframe Compare Date Range","Type":"timeframe-compare-datetime-range","RequestParameters":["CompareStartDate","CompareEndDate"],"Variable":"timeframeCompare"}]},{"Name":"Color","Type":"colors","Variable":"varia-series-color[]"},{"Name":"Include","Variable":"Include","Type":"tag","RequestParameters":"Include","Hide":false},{"Name":"Exclude","Variable":"Exclude","Type":"tag","RequestParameters":"Exclude","Hide":false}],"AlternateCharts":[{"ChartDefinitionId":"webtracker-sessions-timeline","DisplayName":"View as timeline"}]} var myCopy = null;
Tests:
lodash clone deep
var myCopy = _.cloneDeep(MyObject);
JSON parse stringify
var myCopy = JSON.parse(JSON.stringify(MyObject));
Recursive deep copy
var myCopy = recursiveDeepCopy(MyObject);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
lodash clone deep
JSON parse stringify
Recursive deep copy
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):
I'd be happy to explain what's being tested in the provided JSON benchmark. **Benchmark Overview** The benchmark measures the performance of three different methods for creating a deep copy of an object: Lodash's `cloneDeep` function, `JSON.parse(JSON.stringify(MyObject))`, and a custom recursive function. The benchmark creates a complex object `MyObject` with nested properties and then tests each method to create a deep copy of this object. **Options Compared** The three options being compared are: 1. **Lodash's `cloneDeep` function**: A popular utility library that provides a simple way to create deep copies of objects. 2. **`JSON.parse(JSON.stringify(MyObject))`**: A built-in JavaScript method that attempts to create a deep copy of an object by serializing it to JSON and then parsing the JSON back into an object. 3. **Custom recursive function**: A homemade implementation of a recursive function to create a deep copy of the object. **Pros and Cons** 1. **Lodash's `cloneDeep` function**: * Pros: Fast, efficient, and well-tested. * Cons: Requires an additional library dependency (Lodash). 2. **`JSON.parse(JSON.stringify(MyObject))`**: * Pros: Built-in JavaScript method, no dependencies required. * Cons: Can be slow and inefficient for large objects due to the serialization and parsing process. 3. **Custom recursive function**: * Pros: No additional dependencies required, can be highly customizable. * Cons: Requires manual implementation of a recursive function, which can be error-prone. **Library Usage** The benchmark uses Lodash's `cloneDeep` function in its first test case. This library provides a convenient and efficient way to create deep copies of objects. The custom recursive function is not shown in the provided code snippet, but it would typically involve manually implementing a recursive function to traverse the object tree. **Special JS Feature/Syntax** There are no special JavaScript features or syntax used in this benchmark that would require additional explanation. **Other Alternatives** If you're interested in exploring alternative deep copy methods, some other options include: * Using `Object.assign()` with the spread operator (`...`): `var myCopy = Object.assign({}, MyObject);` * Using a library like `immer` or `redux-immutable` for immutable object manipulation * Implementing a custom deep copy function using a loop and recursion Keep in mind that each of these alternatives has its own trade-offs in terms of performance, readability, and dependency requirements.
Related benchmarks:
(sortBy + reverse) vs orderBy desc
Set vs obj
Set vs object tracking
sdasdasdasd123123123
comparision in all deep clone
Comments
Confirm delete:
Do you really want to delete benchmark?