Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
cloneDeep vs JSON stringify + parse (long arr)
(version: 0)
Comparing performance of:
cloneDeep vs JSON
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 obj = Array.from({ length: 100000 }).map((_, i) => i);
Tests:
cloneDeep
_.cloneDeep(obj);
JSON
JSON.parse(JSON.stringify(obj))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
cloneDeep
JSON
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's being tested in this benchmark. **Benchmark Definition** The benchmark is comparing the performance of two methods to create a deep copy of an array: 1. `_.cloneDeep(obj);` from the Lodash library 2. `JSON.parse(JSON.stringify(obj))` These methods are used to create a new, independent copy of the original array, without modifying the original. **What's being tested** The benchmark is measuring which method is faster, in terms of the number of executions per second (ExecutionsPerSecond). **Options compared** We have two options being compared: A) `_.cloneDeep(obj);` from Lodash B) `JSON.parse(JSON.stringify(obj))` **Pros and Cons** ### _.cloneDeep(obj) Pros: * Specifically designed for deep cloning arrays, making it a good choice when you need to create an exact copy. * Often more efficient than other methods due to its optimized implementation. Cons: * Requires the Lodash library, which adds overhead. * May have slower startup times compared to other methods. ### JSON.parse(JSON.stringify(obj)) Pros: * Fast and lightweight, with no additional library dependencies. * Easy to implement, as it's a simple string manipulation technique. Cons: * Not specifically designed for deep cloning arrays, making it less efficient than _.cloneDeep. * Can fail if the original array contains cyclic references (i.e., an array that references itself). **Library: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of helper functions for tasks like array manipulation, object transformation, and more. In this case, `_`.cloneDeep(obj)` is used to create a deep copy of the original array. **Special JS feature or syntax** None mentioned in this benchmark, but it's worth noting that some browsers may have specific optimizations or features that affect performance, such as: * Chrome's `Array.prototype.slice()` method optimization * Firefox's `Array.prototype.map()` method optimization However, these are not directly relevant to the current benchmark. **Other alternatives** If you don't want to use Lodash, there are other libraries and methods available for deep cloning arrays, such as: * `Array.prototype.slice()` * `Array.prototype.concat()` * A custom implementation using a recursive function or a library like Immutable.js Keep in mind that these alternatives may have different performance characteristics compared to _.cloneDeep. In summary, the benchmark is comparing two methods for creating a deep copy of an array: `_.cloneDeep(obj)` from Lodash and `JSON.parse(JSON.stringify(obj))`. The choice between them depends on your specific use case, trade-offs in terms of speed and library dependencies, and potential issues with cyclic references.
Related benchmarks:
Lodash cloneDeep vs Lodash clone vs Array.slice() vs. Object.assign()
Lodash cloneDeep vs. Lodash clone vs. Array.slice() vs. Array.slice(0) vs. Object.assign()
Lodash cloneDeep vs Lodash clone vs Array.splice() vs. Object.assign()
Lodash cloneDeep vs JSON parse
Comments
Confirm delete:
Do you really want to delete benchmark?