[...Array(n).keys()].map(k => undefined) vs Array.apply(null, { length: 4 }) vs Array.from vs Array(n).fill(undefined) vs for loop pushFastest: Array(n).fill(undefined) (47.4x faster)
4,074 ops/sec
487 ops/sec
103 ops/sec
94 ops/sec
86 ops/sec
Fastest: for loop push (6.3x faster)
2,704 ops/sec
2,122 ops/sec
1,037 ops/sec
595 ops/sec
427 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).keys()].map(k => undefined) | |
| Array.apply(null, { length: 4 }) | |
| Array.from | |
| Array(n).fill(undefined) | |
| for loop push |
| Test name | Executions per second |
|---|---|
| ✓ Array(n).fill(undefined) | 2,122 Ops/sec |
| for loop push | 792 Ops/sec |
| Array.from | 720 Ops/sec |
| [...Array(n).keys()].map(k => undefined) | 349 Ops/sec |
| Array.apply(null, { length: 4 }) | 269 Ops/sec |