Replacements for pako
pako is a JavaScript port of zlib. Its install size is about 2.5 MB, and most of what it does is now available as a smaller package or built into the runtime.
fflate
fflate is a much smaller and faster pure JavaScript implementation with synchronous functions close to pako's.
Be careful with the names: pako.deflate produces the zlib format, which is zlibSync in fflate. fflate's deflateSync is raw deflate, the same as pako.deflateRaw.
pako | fflate |
|---|---|
deflate / inflate | zlibSync / unzlibSync |
deflateRaw / inflateRaw | deflateSync / inflateSync |
gzip / ungzip | gzipSync / gunzipSync |
pako.inflate also accepts gzip input. If you relied on that, use decompressSync, which detects zlib, gzip and raw deflate.
import * as pako from 'pako'
import { zlibSync, unzlibSync } from 'fflate'
const compressed = pako.deflate(data)
const compressed = zlibSync(data)
const restored = pako.inflate(compressed)
const restored = unzlibSync(compressed) pako 2's { to: 'string' } option has no counterpart (it was also removed in pako 3). Decode the bytes with strFromU8 from fflate or with TextDecoder.
node:zlib (native, Node.js built-in)
Node ships zlib as node:zlib, with the same formats under the same names: deflateSync / inflateSync for zlib, deflateRawSync / inflateRawSync for raw deflate and gzipSync / gunzipSync for gzip. Asynchronous and streaming versions are also available.
import * as pako from 'pako'
import { deflateSync, inflateSync } from 'node:zlib'
const compressed = pako.deflate(data)
const compressed = deflateSync(data)
const restored = pako.inflate(compressed)
const restored = inflateSync(compressed) Compression Streams API (native)
CompressionStream and DecompressionStream are available in all modern browsers and as globals in Node 18 and later. The 'deflate', 'deflate-raw' and 'gzip' formats match pako.deflate, pako.deflateRaw and pako.gzip. The API is stream-based and asynchronous, so it fits best where you already work with streams or await.
import * as pako from 'pako'
const compressed = pako.gzip(data)
const compressed = new Uint8Array(
await new Response(
new Blob([data]).stream().pipeThrough(new CompressionStream('gzip'))
).arrayBuffer()
)