A Node.js tool to compare different HTTP compression algorithms (gzip, deflate, and Brotli) in terms of compression ratio, processing time, and data savings.
- Tests three compression algorithms: gzip, deflate, and Brotli (br)
- Measures compression ratio, processing time, and data savings
- Uses Express server for realistic testing environment
- Generates human-readable results
- Includes configurable test data size
- Provides detailed performance metrics
- Node.js (version 14 or higher)
- npm (Node Package Manager)
- Clone the repository:
git clone https://github.com/yourusername/compression-test.git
cd compression-test
- Install dependencies:
npm install
This will install the required packages:
- express
- compression
- axios
Run the test suite:
node index.js
The script will:
- Generate test data (default 5.3 MB)
- Test each compression algorithm
- Display results in a formatted table
Here are the results from a test run with 5.3 MB of text data:
Testing different compression algorithms...
Testing gzip compression:
Original content size: 5.3 MB
Testing deflate compression:
Original content size: 5.3 MB
Testing br compression:
Original content size: 5.3 MB
Final Results:
┌─────────┬───────────┬──────────────┬────────────────┬──────────────────┬────────────┬─────────────┐
│ (index) │ algorithm │ originalSize │ compressedSize │ compressionRatio │ timeTaken │ savingsInMB │
├─────────┼───────────┼──────────────┼────────────────┼──────────────────┼────────────┼─────────────┤
│ 0 │ 'gzip' │ '5.3 MB' │ '2.03 MB' │ '61.66%' │ '252.01ms' │ '3.27 MB' │
│ 1 │ 'deflate' │ '5.3 MB' │ '2.03 MB' │ '61.66%' │ '241.10ms' │ '3.27 MB' │
│ 2 │ 'br' │ '5.3 MB' │ '1.89 MB' │ '64.26%' │ '95.31ms' │ '3.40 MB' │
You can modify the test parameters in index.js
:
// Change test data size (in bytes)
const generateTestData = (size = 5 * 1024 * 1024) => {
// ...
};
// Modify compression settings
app.use(compression({
level: 6, // Compression level (0-9)
threshold: 0, // Minimum size for compression
memLevel: 8, // Memory usage level
filter: () => true // Compression filter
}));
Tests a specific compression algorithm with the provided test data.
Parameters:
algorithm
: String - 'gzip', 'deflate', or 'br'testData
: String - The data to compress
Returns: Promise with test results object:
{
algorithm: string, // Compression algorithm used
originalSize: string, // Original size with units
compressedSize: string, // Compressed size with units
compressionRatio: string, // Compression percentage
timeTaken: string, // Processing time in ms
savingsInMB: string // Data saved in MB
}
Generates test data of specified size.
Parameters:
size
: Number - Size in bytes (default: 5MB)
Returns: String - Generated test data
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE.md file for details.
- Express.js team for the compression middleware
- Node.js community for zlib implementation
- Google for the Brotli compression algorithm
If you have any questions or need help with the code, please open an issue in the GitHub repository.