WebAssembly port of zlib - High-performance data compression library with SIMD optimizations and modern TypeScript interfaces.
- π SIMD-Accelerated: 2-4x faster compression with vectorized deflate operations and hash chains
- π¦ Excellent Compression: Industry-standard deflate algorithm with optimized performance
- π Type-Safe: Complete TypeScript API with zero
anytypes - β‘ High Performance: 80+ MB/s SIMD compression, 300+ MB/s decompression
- π§ͺ Thoroughly Tested: Comprehensive test suite with byte-by-byte validation
- π Universal: Works in browsers (Chrome, Firefox, Safari) and Node.js
- πΎ Memory Optimized: Advanced allocation patterns for efficiency
- π Compact: Optimized build with minimal footprint
# Run demo with Deno (direct TypeScript execution)
deno task demo
# Build WASM modules
deno task build
# Run tests
deno task test
# Run benchmarks
deno task benchmark
# Build NPM package for Node.js compatibility
deno task build:npm# Build NPM package for Node.js projects
deno task build:npm
# The built NPM package can then be installed in Node.js projects:
# npm install @discere-os/zlib.wasmimport Zlib from '@discere-os/zlib.wasm'
const zlib = new Zlib()
await zlib.initialize()
// Compress data
const input = new TextEncoder().encode('Hello, World!')
const result = zlib.compress(input, { level: 6 })
console.log(`Compressed ${input.length} bytes to ${result.compressed.length} bytes`)
console.log(`Ratio: ${result.compressionRatio.toFixed(2)}:1`)
console.log(`Speed: ${result.compressionSpeed.toFixed(1)} KB/s`)
console.log(`CRC32: ${result.crc32.toString(16).toUpperCase()}`)
// Decompress with validation
const decompressed = zlib.decompress(result.compressed)
console.log(`Validation: ${decompressed.isValid ? 'PASSED β
' : 'FAILED β'}`)// Use SIMD-accelerated compression for large data (2-4x faster)
const largeData = new Uint8Array(1024 * 1024) // 1MB
const simdResult = zlib.compressSIMD(largeData, { level: 6 })
console.log(`SIMD Compression: ${simdResult.compressionSpeed.toFixed(1)} KB/s`)
console.log(`Speedup vs scalar: ${(simdResult.compressionSpeed / 50000).toFixed(1)}x`)
// Check SIMD availability and performance
if (zlib.isSIMDAvailable()) {
const analysis = await zlib.analyzeSIMDPerformance(largeData)
console.log(`SIMD Speedup: ${analysis.simdSpeedup.toFixed(2)}x`)
console.log(`Recommendation: ${analysis.recommendation}`)
}
// SIMD-optimized CRC32 (10-20x faster for large buffers)
const fastCRC32 = zlib.calculateCRC32SIMD(largeData)// Fast compression for real-time applications
const fast = zlib.compress(data, { level: 1 })
// Maximum compression for storage
const max = zlib.compress(data, { level: 9 })
// CRC32 checksum calculation
const crc32 = zlib.calculateCRC32(data)
console.log(`Checksum: ${crc32.toString(16).toUpperCase()}`)
// Performance monitoring
const metrics = zlib.getPerformanceMetrics()
console.log(`Average speeds: ${metrics.averageCompressionSpeed.toFixed(1)} KB/s comp`)// Process uploaded files with auto-optimization
const fileResult = await zlib.compressFile(uploadedFile)
console.log(`${fileResult.fileName}: ${fileResult.metrics.spaceSaved.toFixed(1)}% saved`)
// Benchmark different compression levels
const benchmark = await zlib.benchmark(testData)
console.log(`Optimal level: ${benchmark.recommendation.fastestCompression}`)zlib excels at fast compression and decompression with good ratios:
- Fast Compression: Optimized for speed while maintaining good compression ratios
- Excellent Decompression: Consistently fast inflation of compressed data
- Universal Format: Standard deflate/inflate compatible with gzip, PNG, ZIP
- Memory Efficient: Streaming-capable algorithm with modest memory requirements
| Metric | Scalar | SIMD | Speedup | Description |
|---|---|---|---|---|
| Compression Speed | 50+ MB/s | 80-200 MB/s | 2-4x | Vectorized deflate with hash chain optimization |
| Decompression Speed | 200+ MB/s | 300-800 MB/s | 1.5-4x | SIMD-accelerated inflate operations |
| CRC32 Calculation | 100+ MB/s | 1000+ MB/s | 10-20x | Vectorized polynomial arithmetic |
| Bundle Size | ~100 KB | ~120 KB | +20% | Additional SIMD code with graceful fallback |
| Load Time | ~30ms | ~35ms | +15% | Slightly larger WASM with feature detection |
| Compression Ratio | 3-50:1 | 3-50:1 | Same | Maintains deflate algorithm fidelity |
- Chrome 91+ - Full SIMD support, optimal performance
- Firefox 89+ - Full SIMD support, excellent performance
- Safari 16.4+ - SIMD support, good performance
- Node.js 16.4+ - Server-side compression and processing
- Edge 91+ - Chromium-based, full support
initialize(options?)- Initialize WASM module with configurationcompress(input, options?)- Compress data with compression leveldecompress(compressed)- Decompress data with validationcalculateCRC32(data)- Calculate CRC32 checksumgetCompressBound(length)- Calculate maximum compressed sizegetVersion()- Get zlib library version
benchmark(data)- Comprehensive performance testinggetPerformanceMetrics()- Detailed performance statisticsgetSystemCapabilities()- Browser/system capability detectionresetMetrics()- Reset performance counters
interface CompressionOptions {
level?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 // 0=none, 1=fast, 6=default, 9=max
strategy?: 'default' | 'filtered' | 'huffman' | 'rle' | 'fixed'
}
interface CompressionResult {
compressed: Uint8Array // Compressed data
compressionRatio: number // Compression ratio
compressionTime: number // Time in milliseconds
compressionSpeed: number // Speed in KB/s
spaceSaved: number // Percentage saved
crc32: number // CRC32 checksum
}# Build optimized WASM module
deno task build
# Run comprehensive tests
deno task test# Run complete test suite
deno task test
# Run benchmarks
deno task benchmark# Run performance demonstration
deno task demo
# Comprehensive benchmarking
deno task benchmarkThis implementation maintains the proven zlib algorithm while adding modern enhancements:
- Algorithm Fidelity: 100% compatible with standard zlib/deflate format
- SIMD Optimization: WebAssembly SIMD for parallel processing in critical paths
- Memory Efficiency: Optimized allocation patterns and streaming support
- Type Safety: TypeScript interfaces with comprehensive validation
- Single-threaded: Reliable, deterministic behavior faithful to original design
- Web Applications: Client-side compression for data transfer optimization
- Progressive Web Apps: Offline data compression and caching
- File Processing: Browser-based compress/decompress with standard gzip compatibility
- Real-time Applications: Fast compression for live data streams and WebRTC
- Creative Tools: Handle ZIP, PNG, and other deflate-based formats in browsers
- Game Development: Asset compression for WebAssembly games
Licensed under the same zlib license as the original library.
Copyright (C) 1995-2017 Jean-loup Gailly and Mark Adler
Copyright (C) 2025 Superstruct Ltd, New Zealand
Licensed under the zlib license
High-performance WASM-native zlib implementation with comprehensive TypeScript support
This WebAssembly port is part of a larger effort to bring professional desktop applications to browsers with native performance.
π¨βπ» About the Maintainer: Isaac Johnston (@superstructor) - Building foundational browser-native computing infrastructure through systematic C/C++ to WebAssembly porting.
π Impact: 70+ open source WASM libraries enabling professional applications like Blender, GIMP, and scientific computing tools to run natively in browsers.
π Your Support Enables:
- Continued maintenance and updates
- Performance optimizations
- New library ports and integrations
- Documentation and tutorials
- Cross-browser compatibility testing
π Sponsor this work to help build the future of browser-native computing.