compression package.
Installation
First, install the required packages:Enabling Compression
Enable compression in your configuration:Configuration Options
For more control, pass configuration options:Available Options
| Option | Type | Default | Description |
|---|---|---|---|
threshold | string | number | 1024 | Minimum response size to compress (bytes or string like “1kb”) |
level | number | 6 | Compression level (0=none, 9=max, -1=default) |
strategy | number | - | Compression strategy for GZIP |
chunkSize | number | 16384 | Chunk size for compression |
memLevel | number | 8 | Memory level for GZIP (1-9) |
How It Works
When a client sends a request with theAccept-Encoding header, the server responds with compressed data:
Compression Algorithms
Express Zod API supports multiple compression algorithms:- Brotli (
br) - Best compression, slower - GZIP (
gzip) - Good compression, fast - Deflate (
deflate) - Older, less efficient
Accept-Encoding header.
What Gets Compressed?
Only responses with compressible content types are compressed:application/json✅text/html✅text/plain✅text/css✅application/javascript✅image/jpeg❌ (already compressed)image/png❌ (already compressed)video/*❌ (already compressed)
Minimum Size Threshold
Small responses aren’t worth compressing due to overhead. Set a threshold:1kb(1024 bytes) - Default, good for most APIs512(512 bytes) - More aggressive2kb(2048 bytes) - Less aggressive
Compression Levels
Balance compression ratio vs. CPU usage:| Level | Speed | Compression | Use Case |
|---|---|---|---|
| 1 | Fastest | Minimal | High-traffic APIs, CPU-constrained |
| 6 | Balanced | Good | Default, most use cases |
| 9 | Slowest | Maximum | Low-traffic, bandwidth-constrained |
Real-World Example
From the Express Zod API example:Testing Compression
Verify compression is working:- Open Network tab
- Make request to your API
- Check response headers for
Content-Encoding: gziporContent-Encoding: br - Compare “Size” vs “Transferred” columns
Performance Impact
Benefits
- Reduced bandwidth: 60-80% smaller responses for JSON
- Faster transfer: Especially on slow connections
- Lower costs: Reduced data transfer fees
Trade-offs
- CPU usage: Compression takes processing time
- Latency: Minimal increase for compression/decompression
- Memory: Buffering during compression
Benchmarks
Typical JSON response (10KB uncompressed):| Algorithm | Size | Ratio | Compression Time |
|---|---|---|---|
| None | 10KB | - | - |
| GZIP (level 6) | 2.5KB | 75% | ~1ms |
| Brotli | 2.0KB | 80% | ~2ms |
When NOT to Use Compression
Already Compressed Content
Images, videos, and PDFs are already compressed. Don’t compress them again.
Very Small Responses
Responses under 150 bytes may become larger when compressed due to overhead.
CPU-Constrained Servers
If CPU is your bottleneck, compression may hurt more than help.
Streaming Responses
Streaming with compression is complex. Consider pre-compression instead.
Advanced Configuration
Environment-Based
Custom Filter
While Express Zod API doesn’t expose the filter directly, you can usebeforeRouting:
Monitoring Compression
Add logging to track compression effectiveness:Best Practices
Always Enable in Production
Compression is a free performance win for most APIs. Enable it.
Use Default Settings
The defaults (level 6, threshold 1KB) work well for most cases.
Test with Real Data
Benchmark with your actual API responses to find optimal settings.
Monitor CPU Usage
If CPU spikes after enabling compression, reduce the level or increase threshold.
Common Issues
Compression Not Working
Check:- Client sends
Accept-Encoding: gzip, deflate, br - Response is compressible (JSON, text)
- Response size exceeds threshold
- No other middleware interferes
Performance Degradation
If compression slows your API:- Lower compression level (6 → 3)
- Increase threshold (1KB → 5KB)
- Profile to find bottleneck