compress
bun add @stopcock/compressFour 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)Real-world patterns
Section titled “Real-world patterns”Compressing data for storage
Section titled “Compressing data for storage”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))Comparing algorithms
Section titled “Comparing algorithms”import { RLE, LZ77, Huffman, Deflate } from '@stopcock/compress'
const data = new TextEncoder().encode(repeatedText)
RLE.encode(data).length // Best for runs of identical bytesLZ77.encode(data).length // Good for repeated sequencesHuffman.encode(data).encoded.length // Good for skewed frequency distributionsDeflate.compress(data).length // Best general-purpose (LZ77 + Huffman)Deflate
Section titled “Deflate”Combines LZ77 + Huffman for general-purpose compression.
Deflate.compress(data: Uint8Array): Uint8ArrayDeflate.decompress(data: Uint8Array): Uint8ArraySliding-window compression. Finds repeated sequences and replaces them with back-references.
LZ77.encode(data: Uint8Array, windowSize?: number): Uint8ArrayLZ77.decode(data: Uint8Array): Uint8ArrayHuffman
Section titled “Huffman”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): Uint8ArrayRun-length encoding. Best for data with long runs of identical bytes.
RLE.encode(data: Uint8Array): Uint8ArrayRLE.decode(data: Uint8Array): Uint8Array