Skip to content

Instantly share code, notes, and snippets.

@Nooshu
Created February 20, 2026 12:52
Show Gist options
  • Select an option

  • Save Nooshu/0dd55a4ba67da0a6d0053dc0e2884ba8 to your computer and use it in GitHub Desktop.

Select an option

Save Nooshu/0dd55a4ba67da0a6d0053dc0e2884ba8 to your computer and use it in GitHub Desktop.
The core compression code I use on my blog for compressing HTML (and other text assets) to Brotli level 11.
import { brotliCompressSync } from 'zlib';
/** Default Brotli compression level (0–11). Level 11 gives best ratio, slowest. */
export const BROTLI_LEVEL = 11;
/**
* Compress a buffer with Brotli.
* @param {Buffer | Uint8Array | string} input - Input buffer, TypedArray, or string (UTF-8)
* @param {number} [level=BROTLI_LEVEL] - Brotli level 0–11
* @returns {Buffer} Compressed buffer
*/
export function brotliCompress(input, level = BROTLI_LEVEL) {
const buffer = Buffer.isBuffer(input) ? input : Buffer.from(input);
return brotliCompressSync(buffer, { level });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment