new Array(500).fill({}).map(()=> ({a:5}))
Array.from({ length: 500 }).map(()=> ({a:5}))
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
new Array() | |
Array.from() |
Test name | Executions per second |
---|---|
new Array() | 323426.7 Ops/sec |
Array.from() | 44047.8 Ops/sec |
Let's break down what's being tested in the provided benchmark.
Benchmark Definition
The benchmark is designed to compare two approaches for creating filled arrays:
fill()
method is used to fill the array with a specified value.Options Compared
In this benchmark, we have two test cases:
fill()
method.Array.from()
method to create a new array from an iterable source (in this case, an object).Pros and Cons
Here's a brief summary of the pros and cons of each approach:
new Array():
Pros:
Cons:
fill()
methodfill()
internallyArray.from():
Pros:
Cons:
Library/Function Used
In this benchmark, we're using the following libraries/functions:
Array.from()
: This is a built-in JavaScript function introduced in ES6.new Array()
and .fill()
: These are part of the traditional JavaScript array creation and manipulation.Special JS Feature/Syntax
There's no special feature or syntax being used in this benchmark. However, if we were to expand the scope, we could explore other aspects of JavaScript performance, such as:
let
or const
instead of var
for...of
loops instead of traditional for
loopsOther Alternatives
If you're interested in exploring alternative approaches, here are a few options:
These alternatives might not directly relate to array creation and benchmarking, but they showcase the diversity of options available for building web applications.
In summary, this benchmark provides a straightforward comparison between two approaches for creating filled arrays: traditional new Array()
with .fill()
, and modern Array.from()
. The results will help you understand which approach performs better in your specific use case.