<script src="https://cdn.jsdelivr.net/npm/lodash@1.3.1/lodash.min.js"></script>
var N = 10000;
var stringItems = [ Array(N).keys() ].map( i => "foo");
var objectItems = [ Array(N).keys() ].map( i => { return { bar: "foo" } });
_.cloneDeep(stringItems);
_.cloneDeep(objectItems);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Clone deep strings | |
Clone deep objects |
Test name | Executions per second |
---|---|
Clone deep strings | 2161.6 Ops/sec |
Clone deep objects | 6.2 Ops/sec |
Let's dive into the benchmark and explain what's being tested.
The provided JSON represents a JavaScript microbenchmarking test case on MeasureThat.net. The test compares the performance of two approaches: cloning an array of strings with Lodash's cloneDeep
function versus cloning an array of objects with the same function.
What is being compared?
cloneDeep
function.cloneDeep
function.Options being compared:
Pros and Cons of each approach:
Lodash library:
The cloneDeep
function is a utility function from Lodash that creates a deep copy of an object or array. The purpose of this function is to recursively traverse the input data structure, creating new copies of all nested elements, and return a new, independent copy of the original data structure.
Special JS feature: None mentioned
The benchmark does not rely on any special JavaScript features or syntax that would make it less applicable to other programming languages or environments.
Other alternatives:
If you wanted to create similar benchmarks for other programming languages or libraries, you could consider the following:
String.prototype.split()
method with an empty array as a separator.JSON.parse(JSON.stringify(obj))
syntax, which creates a shallow copy of an object.Keep in mind that these alternatives would require modifications to the benchmark code and may not provide the same level of representativeness as the original Lodash-based approach.