const test = Array.from({ length: 10 }, () => 0)
const test = new Array(10).fill(0, 0, 10)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
array 1 | |
array 2 |
Test name | Executions per second |
---|---|
array 1 | 1042146.8 Ops/sec |
array 2 | 3920998.2 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 benchmark is comparing two ways to create an array in JavaScript:
Array.from()
: This method creates a new array by iterating over an iterable object (in this case, an empty object with a length of 10) and mapping each iteration to produce a value.new Array()
constructor with three arguments: length
, fillValue
, and startIndex
. In this case, length
is set to 10, fillValue
is set to 0, and startIndex
is set to 0.Options compared
The two approaches are being compared in terms of their performance. Specifically, the benchmark measures how many executions per second (ExecutionsPerSecond) each approach can handle on a given machine.
Pros and cons of each approach:
Other considerations:
Library and special features
There doesn't seem to be any libraries being used in this benchmark, nor are there any special JavaScript features or syntax being tested. The focus is purely on comparing two different approaches to creating an array in JavaScript.
Other alternatives:
If you're looking for alternative methods to create an array in JavaScript, some options include:
[...]
) to create a new array:const arr = [...new Array(10)];
Array()
with two arguments (length
and init
):const arr = new Array(10, 0);
However, these alternatives may not be as performant or memory-efficient as the methods being compared in this benchmark.
I hope this explanation helps you understand what's going on in this JavaScript microbenchmark!