new Array(500)
Array.from({ length: 500 })
let a = [];
a.length = 500;
[].fill(undefined, 0, 500)
[].fill(void 0, 0, 500)
new Array().fill(0, 0, 500)
Array.from({ length: 500 }, () => 0)
[].fill(0, 0, 500)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
new Array() | |
Array.from() | |
[] + length | |
[] + fill undefined | |
[] + fill void | |
new Array() zeroed | |
Array.from zeroes | |
[] + fill zeroes |
Test name | Executions per second |
---|---|
new Array() | 1778515328.0 Ops/sec |
Array.from() | 903696.9 Ops/sec |
[] + length | 91637984.0 Ops/sec |
[] + fill undefined | 107701344.0 Ops/sec |
[] + fill void | 132005272.0 Ops/sec |
new Array() zeroed | 93331232.0 Ops/sec |
Array.from zeroes | 818759.8 Ops/sec |
[] + fill zeroes | 131934096.0 Ops/sec |
I'll break down the provided JSON and explain what's being tested, compared options, pros/cons, library usage, special JS features, and other considerations.
Benchmark Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The benchmark in question compares different ways of creating an array with 500 elements.
Test Cases
There are eight test cases:
new Array(500)
Array.from({ length: 500 })
let a = [];\r\na.length = 500;
(not exactly creating an array, but using the length
property to set the size)[].fill(undefined, 0, 500)
[].fill(void 0, 0, 500)
new Array().fill(0, 0, 500)
Array.from({ length: 500 }, () => 0)
[].fill(0, 0, 500)
Options Compared
Each test case compares a different way of creating an array with 500 elements:
new Array(500)
and Array.from({ length: 500 })
let a = [];\r\na.length = 500;
(not exactly creating an array, but using the length
property)[].fill(undefined, 0, 500)
and [].fill(void 0, 0, 500)
fill()
methodnew Array().fill(0, 0, 500)
and [].fill(0, 0, 500)
Library Usage
None of the test cases use any external libraries.
Special JS Features
Some test cases use special JavaScript features:
These features are widely supported in modern browsers and environments, but may not work in older browsers or environments that don't support them.
Other Considerations
let a = [];\r\na.length = 500;
) are not exactly creating an array, but using the length
property to set the size.Alternatives
If you're looking for alternatives to MeasureThat.net, some options include:
Keep in mind that these alternatives may not provide the same level of customization or control as MeasureThat.net.