<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var obj = Array.from({ length: 100000 }).map((_, i) => i);
_.cloneDeep(obj);
JSON.parse(JSON.stringify(obj))
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
cloneDeep | |
JSON |
Test name | Executions per second |
---|---|
cloneDeep | 299.5 Ops/sec |
JSON | 324.6 Ops/sec |
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:
_.cloneDeep(obj);
from the Lodash libraryJSON.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
Pros:
Cons:
Pros:
Cons:
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:
Array.prototype.slice()
method optimizationArray.prototype.map()
method optimizationHowever, 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()
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.