Skip to content

compress

Terminal window
bun add @stopcock/compress

Four compression algorithms, each as its own namespace. All work on Uint8Array.

import { Deflate } from '@stopcock/compress'
const compressed = Deflate.compress(data)
const original = Deflate.decompress(compressed)
import { Deflate } from '@stopcock/compress'
const data = new TextEncoder().encode(JSON.stringify(largePayload))
const compressed = Deflate.compress(data)
// Store compressed bytes, typically 60-80% smaller for JSON
const original = Deflate.decompress(compressed)
const restored = JSON.parse(new TextDecoder().decode(original))
import { RLE, LZ77, Huffman, Deflate } from '@stopcock/compress'
const data = new TextEncoder().encode(repeatedText)
RLE.encode(data).length // Best for runs of identical bytes
LZ77.encode(data).length // Good for repeated sequences
Huffman.encode(data).encoded.length // Good for skewed frequency distributions
Deflate.compress(data).length // Best general-purpose (LZ77 + Huffman)

Combines LZ77 + Huffman for general-purpose compression.

Deflate.compress(data: Uint8Array): Uint8Array
Deflate.decompress(data: Uint8Array): Uint8Array

Sliding-window compression. Finds repeated sequences and replaces them with back-references.

LZ77.encode(data: Uint8Array, windowSize?: number): Uint8Array
LZ77.decode(data: Uint8Array): Uint8Array

Optimal prefix coding. Assigns shorter bit sequences to more frequent bytes.

Huffman.encode(data: Uint8Array): { encoded: Uint8Array; tree: HuffmanTree }
Huffman.decode(data: Uint8Array, tree: HuffmanTree): Uint8Array

Run-length encoding. Best for data with long runs of identical bytes.

RLE.encode(data: Uint8Array): Uint8Array
RLE.decode(data: Uint8Array): Uint8Array