new Array(500).fill(null)
Array.from({ length: 500 }, () => null)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
new Array() | |
Array.from() |
Test name | Executions per second |
---|---|
new Array() | 1396500.6 Ops/sec |
Array.from() | 54245.7 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks!
What is being tested?
The provided benchmark tests two ways to create filled arrays in JavaScript:
fill()
method.Options being compared
The two options being compared are:
new Array()
: A traditional way of creating arrays in JavaScript.Array.from()
: A more modern way of creating arrays, introduced in ECMAScript 2015 (ES6).Pros and cons of each approach:
Array.from()
due to the overhead of creating an empty array and then filling it.new Array()
because it avoids the overhead of creating an empty array.Library and purpose
In this benchmark, a library is not explicitly mentioned. However, Array.from()
uses a built-in JavaScript function to create an array from an iterable.
Special JS feature or syntax
There are no special JavaScript features or syntaxes used in this benchmark other than the use of ECMAScript 2015 (ES6) syntax for Array.from()
.