Skip to content

Instantly share code, notes, and snippets.

@scarf005
Created July 25, 2024 06:08
Show Gist options
  • Save scarf005/22c2ab232d7a6332176032c2ba338f4f to your computer and use it in GitHub Desktop.
Save scarf005/22c2ab232d7a6332176032c2ba338f4f to your computer and use it in GitHub Desktop.
for (const N of [10_000, 100_000, 1_000_000]) {
Deno.bench("Array.fill()", { group: `${N}`, baseline: true }, () => {
new Array(N).fill(0)
})
Deno.bench("Array.from()", { group: `${N}` }, () => {
Array.from({ length: N }, () => 0)
})
}
$ deno bench
cpu: Apple M1 Pro
runtime: deno 1.45.2 (aarch64-apple-darwin)

file:///Users/nemo/repo/deno-playground/fill_bench.ts
benchmark         time (avg)        iter/s             (min … max)       p75       p99      p995
------------------------------------------------------------------ -----------------------------

group 10000
Array.fill()       8.72 µs/iter     114,693.8     (8.54 µs … 8.91 µs) 8.74 µs 8.91 µs 8.91 µs
Array.from()     306.86 µs/iter       3,258.8 (301.46 µs … 418.21 µs) 306.71 µs 350.33 µs 359.83 µs

summary
  Array.fill()
   35.2x faster than Array.from()

group 100000
Array.fill()      76.32 µs/iter      13,103.4  (67.42 µs … 228.58 µs) 71.08 µs 145.5 µs 151.25 µs
Array.from()        3.1 ms/iter         323.0     (3.03 ms … 3.17 ms) 3.12 ms 3.16 ms 3.17 ms

summary
  Array.fill()
   40.57x faster than Array.from()

group 1000000
Array.fill()      789.9 µs/iter       1,266.0  (779.5 µs … 861.38 µs) 791.5 µs 826.88 µs 846.92 µs
Array.from()      31.06 ms/iter          32.2   (30.93 ms … 31.75 ms) 31.05 ms 31.75 ms 31.75 ms

summary
  Array.fill()
   39.32x faster than Array.from()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment