const fooArr = new Array();
for(let i=0;i<100;i++) {
fooArr.push(i);
}
const other = Array.from(fooArr);
const fooArr = new Array();
for(let i=0;i<100;i++) {
fooArr.push(i);
}
const other = [fooArr];
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.from | |
Spread |
Test name | Executions per second |
---|---|
Array.from | 548935.2 Ops/sec |
Spread | 755429.1 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks and explore what's being tested on MeasureThat.net.
What is being tested?
The provided JSON represents two benchmark tests: Array: Array.from vs Spread
. The test compares the performance of two approaches to create an array from a given array:
Array.from()
: This method takes an iterable (e.g., an array) and returns a new array containing all its elements....
): This syntax is used to extract elements from an array and assign them to another array.Options compared
The test compares the performance of these two approaches in the following scenarios:
push()
.Pros and cons of each approach
Array.from()
:...
):Library usage
Neither test case uses any external libraries or dependencies. The Array.from()
method is a built-in JavaScript function, while the spread operator (...
) is a syntax feature introduced in ECMAScript 2015 (ES6).
Special JS features or syntax
The tests do not use any special JavaScript features or syntax beyond what's available in modern browsers.
Alternatives
If you're interested in exploring alternative approaches for creating arrays, consider the following:
Array.prototype.slice()
: This method returns a shallow copy of the original array.Array.prototype.concat()
: This method concatenates two or more arrays into a new array.map()
and reduce()
methods: These methods can be used to create arrays from iterables, but may have different performance characteristics.Keep in mind that these alternatives might not be as concise or efficient as the spread operator (...
).
The provided benchmark results show that the Chrome Mobile 107 browser performs better with the spread operator (Spread
) for this specific test case. However, it's essential to note that benchmarking results can vary depending on the specific use case, hardware, and environment.