new Array(500).fill(0).map((_, i) => i)
Array.from({ length: 500 }, (_, i) => i)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
new Array().map() | |
Array.from() |
Test name | Executions per second |
---|---|
new Array().map() | 125639.0 Ops/sec |
Array.from() | 200741.1 Ops/sec |
Let's dive into the explanation of the provided benchmark.
What is being tested?
The benchmark compares two approaches to creating filled arrays: Array.from()
and new Array().map()
. The goal is to determine which approach is faster in JavaScript.
Options compared:
Array.from()
: This method creates a new array from an iterable (in this case, an object with a length
property). It's designed to be more flexible and efficient than traditional array creation methods.new Array().map()
: This approach uses the map()
method to create a new array by applying a provided function to each element of an existing array (or in this case, creating a new empty array using new Array()
).Pros and cons:
Array.from()
:new Array().map()
:Array.from()
.Library:
There is no explicit library mentioned in the benchmark definition or test cases. However, some browsers may use additional libraries or polyfills to support newer JavaScript features like Array.from()
.
Special JS feature/syntax:
There are no special JavaScript features or syntaxes used in this benchmark beyond what's provided by the standard language specification.