Skip to content

Instantly share code, notes, and snippets.

@bluwy
Last active January 1, 2025 08:44
Show Gist options
  • Save bluwy/04b04a5ddae47f157881802fa56da1a7 to your computer and use it in GitHub Desktop.
Save bluwy/04b04a5ddae47f157881802fa56da1a7 to your computer and use it in GitHub Desktop.
zlib.gunzip vs DecompressionStream performance benchmark
┌─────────┬───────────────────────────────────────────┬──────────────────────┬─────────────────────┬────────────────────────────┬───────────────────────────┬─────────┐
│ (index) │ Task name │ Latency average (ns) │ Latency median (ns) │ Throughput average (ops/s) │ Throughput median (ops/s) │ Samples │
├─────────┼───────────────────────────────────────────┼──────────────────────┼─────────────────────┼────────────────────────────┼───────────────────────────┼─────────┤
│ 0 │ 'zlib gunzip (from node buffer)' │ '1921050.70 ± 3.04%' │ '1751584.05' │ '550 ± 1.31%' │ '571' │ 521 │
│ 1 │ 'DecompressionStream (from array buffer)' │ '2922942.41 ± 4.29%' │ '2149625.00' │ '392 ± 3.36%' │ '465' │ 343 │
│ 2 │ 'DecompressionStream (from stream)' │ '2904491.21 ± 4.31%' │ '2123957.99' │ '395 ± 3.37%' │ '471' │ 345 │
└─────────┴───────────────────────────────────────────┴──────────────────────┴─────────────────────┴────────────────────────────┴───────────────────────────┴─────────┘
import { readFileSync } from 'fs'
import { Bench } from 'tinybench'
import zlib from 'zlib'
import util from 'util'
const gunzip = util.promisify(zlib.gunzip)
const tgzNodeBuffer = readFileSync('./@babel__core-7.26.0.tgz')
const tgzArrayBuffer = tgzNodeBuffer.buffer
const tgzReadableStreams = Array.from(
{ length: 2000 },
() =>
new ReadableStream({
start(controller) {
controller.enqueue(new Uint8Array(tgzArrayBuffer))
controller.close()
},
})
)
let i = 0
const bench = new Bench({
time: 1000,
warmupTime: 500,
iterations: 50,
warmupIterations: 10,
})
bench
.add('zlib gunzip (from node buffer)', async () => {
await gunzip(tgzNodeBuffer)
})
.add('DecompressionStream (from array buffer)', async () => {
await new Response(
new ReadableStream({
start(controller) {
controller.enqueue(new Uint8Array(tgzArrayBuffer))
controller.close()
},
}).pipeThrough(new DecompressionStream('gzip'))
).arrayBuffer()
})
.add('DecompressionStream (from stream)', async () => {
await new Response(
tgzReadableStreams[i++].pipeThrough(new DecompressionStream('gzip'))
).arrayBuffer()
})
await bench.run()
console.table(bench.table())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment