[...Array(n)] vs Array(n).fill(null) vs Array.from({ length: n }) vs Array.from({ length:n }, (item, i) => i) vs while(up--) myArray.push(Math.random());Fastest: Array(n).fill(null) (1.5x faster)
10,808 ops/sec
10,625 ops/sec
10,129 ops/sec
7,852 ops/sec
7,369 ops/sec
Results show median performance across all community submissions. Run this benchmark yourself to verify on your device.
| Test case name | Result |
|---|---|
| [...Array(n)] | |
| Array(n).fill(null) | |
| Array.from({ length: n }) | |
| Array.from({ length:n }, (item, i) => i) | |
| while(up--) myArray.push(Math.random()); |
| Test name | Executions per second |
|---|---|
| ✓ Array(n).fill(null) | 108,739 Ops/sec |
| [...Array(n)] | 107,696 Ops/sec |
| while(up--) myArray.push(Math.random()); | 60,162 Ops/sec |
| Array.from({ length: n }) | 20,517 Ops/sec |
| Array.from({ length:n }, (item, i) => i) | 20,048 Ops/sec |