What is brotli-size?
The brotli-size npm package is used to calculate the Brotli-compressed size of a given string or buffer. Brotli is a compression algorithm developed by Google that is particularly effective for web content compression.
What are brotli-size's main functionalities?
Calculate Brotli size of a string
This feature allows you to calculate the Brotli-compressed size of a given string synchronously. The code sample demonstrates how to import the brotli-size package, define a string, and then calculate and log its Brotli-compressed size.
const brotliSize = require('brotli-size');
const input = 'Hello, world!';
const size = brotliSize.sync(input);
console.log(`Brotli size: ${size} bytes`);
Calculate Brotli size of a buffer
This feature allows you to calculate the Brotli-compressed size of a given buffer synchronously. The code sample demonstrates how to import the brotli-size package, create a buffer from a string, and then calculate and log its Brotli-compressed size.
const brotliSize = require('brotli-size');
const input = Buffer.from('Hello, world!');
const size = brotliSize.sync(input);
console.log(`Brotli size: ${size} bytes`);
Asynchronous calculation of Brotli size
This feature allows you to calculate the Brotli-compressed size of a given string asynchronously. The code sample demonstrates how to import the brotli-size package, define a string, and then calculate and log its Brotli-compressed size using a promise.
const brotliSize = require('brotli-size');
const input = 'Hello, world!';
brotliSize(input).then(size => {
console.log(`Brotli size: ${size} bytes`);
});
Other packages similar to brotli-size
gzip-size
The gzip-size package calculates the Gzip-compressed size of a given string or buffer. It is similar to brotli-size but uses the Gzip compression algorithm instead of Brotli. Gzip is widely supported and commonly used for web content compression.
zlib
The zlib package is a core Node.js module that provides compression and decompression functionalities using various algorithms, including Gzip and Deflate. While it is more versatile and powerful than brotli-size, it requires more setup and configuration to calculate compressed sizes.
compress-brotli
The compress-brotli package provides Brotli compression and decompression functionalities. It is more comprehensive than brotli-size, offering more control over the compression process, but it is also more complex to use for simple size calculations.
brotli-size
Get the brotli compressed size of a string or buffer.
Install
$ npm install --save brotli-size
Usage
var brotliSize = require('brotli-size');
var str = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam feugiat, mauris non aliquam pretium, libero nulla ultrices lacus, nec varius est purus sit amet dolor.';
console.log(str.length);
console.log(brotliSize.sync(str));
API
brotliSize.sync(input)
input
Type: string
, buffer
brotliSize.stream()
Returns a passthrough stream. The stream emits a brotli-size
event and
has a brotliSize
property.
Related
- gzip-size - Heavily inspired by
this module. Thank you for the inspiration!