const miArray1 = Array(100).fill().map((el, i)=> i)
const miArray1 = Array(100).fill().map((el, i)=> i)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
fill | |
spread |
Test name | Executions per second |
---|---|
fill | 1409004.8 Ops/sec |
spread | 1445295.8 Ops/sec |
I'll break down the explanation into smaller sections to make it easier to understand.
Benchmark Definition JSON
The provided JSON represents a benchmark definition for measuring the performance of JavaScript array creation methods. The key points from this JSON are:
Name
: The name of the benchmark, which is "array creation comparison 2".Description
and Script Preparation Code
, Html Preparation Code
: These fields are empty, indicating that no additional description or preparation code is required for this benchmark.Individual Test Cases
There are two test cases defined:
Benchmark Definition
specifies a JavaScript expression that creates an array using the .fill()
method: const miArray1 = Array(100).fill().map((el, i) => i)
.Benchmark Definition
: const miArray1 = Array(100).fill().map((el, i) => i)
.What are we testing?
We're testing the performance of two array creation methods in JavaScript: .fill()
and the spread operator (...
).
Options compared
The options being compared are:
.fill()
: A method that fills an array with a specified value....
): An operator used to create a new array by spreading elements from another array.Pros and Cons of each approach:
.fill()
map()
call to get the desired output....
).fill()
.Library: map()
The map()
function is a built-in JavaScript function that applies a provided function to each element of an array, returning a new array with the results. In this benchmark, we're using map()
to transform the filled array into an array of indices.
Special JS feature/syntax: None
There's no special JavaScript feature or syntax used in these benchmarks.
Other alternatives
If you wanted to test alternative array creation methods, you could consider:
.slice()
: A method that creates a shallow copy of a portion of an array.new Array(size)
: A constructor function used to create a new empty array.However, these alternatives might not be as relevant for this specific benchmark, which focuses on the performance of .fill()
and the spread operator.