var mixedArray = [];
for (let i = 0; i < 333; i++) {
mixedArray.push("hello", true, 7);
}
var other = mixedArray.slice();
var other = [ mixedArray ]
var other = Array.from(mixedArray);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.slice | |
spread operator | |
Array.from |
Test name | Executions per second |
---|---|
Array.prototype.slice | 4885315.0 Ops/sec |
spread operator | 4698319.0 Ops/sec |
Array.from | 2253319.5 Ops/sec |
Let's break down what's being tested in the provided JSON.
Benchmark Definition
The benchmark is designed to compare three different approaches for creating a shallow copy of an array with mixed data types:
Array.prototype.slice()
...
)Array.from()
Options Compared
Each option is compared in terms of execution speed, measured in executions per second.
Pros and Cons of Each Approach
Array.prototype.slice()
:...
)Array.from()
:slice()
since it creates a new array, and not all browsers support its latest versions.Library Used
None explicitly mentioned in the provided JSON. However, it's likely that the benchmark uses built-in JavaScript functions or polyfills to ensure compatibility with older browsers.
Special JS Feature/Syntax
None mentioned in the provided JSON. The spread operator is a standard ES6 feature and Array.from()
is widely supported.
Other Alternatives
If these three approaches are not suitable, other alternatives for creating shallow copies of arrays include:
Array.prototype.slice.call()
Array.prototype.map()
However, these alternatives might be slower or less efficient than the benchmark's current options.
Now that we've analyzed the JSON, let's move on to discussing how to prepare and run this benchmark using MeasureThat.net.