Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object Deep Copy e
(version: 0)
Produce a deep copy of a Javascript object where nested objects are not simply references to the originals.
Comparing performance of:
Recursive Deep Copy vs JSON Deep Copy vs lodash clone
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.js"></script> <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
Script Preparation code:
function recursiveDeepCopy(obj) { return Object.keys(obj).reduce((v, d) => Object.assign(v, { [d]: (obj[d].constructor === Object) ? recursiveDeepCopy(obj[d]) : obj[d] }), {}); } function jsonDeepCopy(o) { return JSON.parse(JSON.stringify(o)); } var dimensions = [{ "dimensions": [{ "runtime": { "common": { "client": null, "server": null } } }] }]
Tests:
Recursive Deep Copy
var dimensionsCopy = recursiveDeepCopy(dimensions);
JSON Deep Copy
var dimensionsCopy = jsonDeepCopy(dimensions);
lodash clone
var dimensionsCopy = _.cloneDeep(dimensions);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Recursive Deep Copy
JSON Deep Copy
lodash clone
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):
**Benchmark Explanation** The provided JSON represents a JavaScript microbenchmark test case on MeasureThat.net. The goal of the benchmark is to measure the performance of different methods for creating a deep copy of a nested object. **Options Compared** Three options are compared in this benchmark: 1. **Recursive Deep Copy**: This method uses a recursive function `recursiveDeepCopy` to create a deep copy of the object. It iterates through each property of the object, and if the property is an object itself, it recursively calls the same function on that property. 2. **JSON Deep Copy**: This method uses the `JSON.parse()` and `JSON.stringify()` functions to create a deep copy of the object. The `JSON.parse()` function parses a JSON string into a JavaScript object, while `JSON.stringify()` converts a JavaScript object into a JSON string. By passing an object to `JSON.stringify()`, it creates a deep copy of that object. 3. **lodash clone**: This method uses the `cloneDeep` function from the Lodash library to create a deep copy of the object. **Pros and Cons** * **Recursive Deep Copy**: + Pros: Simple implementation, no dependencies required. + Cons: Can be slower than other methods due to recursive function calls. * **JSON Deep Copy**: + Pros: Fast and efficient, uses built-in JavaScript functions. + Cons: Requires JSON serialization/deserialization, which can add overhead. * **lodash clone**: + Pros: Fast and efficient, uses a well-tested library. + Cons: Requires an additional dependency (Lodash). **Library Usage** The `cloneDeep` function from Lodash is used in the "lodash clone" test case. Lodash is a popular JavaScript library that provides utility functions for tasks such as array manipulation, string formatting, and more. **Special JS Feature/Syntax** None of the benchmark options use any special JavaScript features or syntax beyond standard language constructs. **Other Alternatives** If you were to implement your own deep copying method, other alternatives might include: * Using `Object.assign()` with an empty object to create a copy of each property * Using `Array.prototype.map()` and `Array.prototype.reduce()` to iterate through the object's properties and create a new object * Using a library like Immutable.js or js-obj-deep-copy for deep copying Keep in mind that these alternatives might have varying degrees of performance, readability, and maintainability compared to the benchmarked options.
Related benchmarks:
Object Deep Copy
Object Deep Copy test
Object Deep Copy 2
Object Deep Copy vs structuredClone
Comments
Confirm delete:
Do you really want to delete benchmark?