Skip to content

Instantly share code, notes, and snippets.

@manzt
Created May 30, 2024 16:33
Show Gist options
  • Save manzt/89f10957c55e571678025de61e4be30e to your computer and use it in GitHub Desktop.
Save manzt/89f10957c55e571678025de61e4be30e to your computer and use it in GitHub Desktop.
import { assert } from "jsr:@std/assert@0.225.3";
import * as fflate from "npm:fflate@0.8.2";
import * as pako from "npm:pako@2.1.0";
async function decode_stream(stream: ReadableStream) {
const reader = stream
.pipeThrough(new DecompressionStream("gzip"))
.getReader();
let bytes: Uint8Array;
{
const result = await reader.read();
assert(!result.done, "ReadableStream must have data");
bytes = result.value;
}
{
const result = await reader.read();
assert(result.done, "ReadableStream must have no more data");
}
return bytes;
}
const base = new URL(
"https://raw.githubusercontent.com/zarr-developers/zarr_implementations/5dc998ac72/examples/zarr.zr/gzip/.zarray",
);
const BYTES = await fetch(new URL("0.0.0", base))
.then((r) => r.arrayBuffer())
.then((b) => new Uint8Array(b));
const REFERENCE = fflate.gunzipSync(BYTES);
Deno.bench("decode_stream", { baseline: true }, async () => {
const stream = new ReadableStream({
start(controller) {
controller.enqueue(BYTES);
controller.close();
},
});
const result = await decode_stream(stream);
assert(result.length === REFERENCE.length);
});
Deno.bench("fflate.gunzip", () => {
const result = fflate.gunzipSync(BYTES);
assert(result.length === REFERENCE.length);
});
Deno.bench("pako.inflate", () => {
const result = pako.inflate(BYTES);
assert(result.length === REFERENCE.length);
});
@manzt
Copy link
Author

manzt commented May 30, 2024

❯ deno bench -A bench_gzip_decoder.ts
Check file:///Users/manzt/demos/higlass-demo/bench_gzip_decoder.ts
cpu: Apple M3 Max
runtime: deno 1.44.0 (aarch64-apple-darwin)

file:///Users/manzt/demos/higlass-demo/bench_gzip_decoder.ts
benchmark          time (avg)        iter/s             (min … max)       p75       p99      p995
------------------------------------------------------------------- -----------------------------
decode_stream      40.44 µs/iter      24,726.8  (36.83 µs … 239.71 µs) 40.88 µs 56.58 µs 76.71 µs
fflate.gunzip      64.24 µs/iter      15,567.6   (62.5 µs … 242.88 µs) 64.21 µs 76.58 µs 80.96 µs
pako.inflate      106.49 µs/iter       9,390.3 (102.04 µs … 256.96 µs) 105.83 µs 133.33 µs 152.88 µs

summary
  decode_stream
   1.59x faster than fflate.gunzip
   2.63x faster than pako.inflate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment