Skip to content

Commit

Permalink
Docs(/techniques/compression): Brotli compression performance
Browse files Browse the repository at this point in the history
This adds notes on  Brotli compression options and provides an example.

Closes issue nestjs#2834
  • Loading branch information
Eugene Lee committed Oct 20, 2023
1 parent f07a58a commit f0622a7
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion content/techniques/compression.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,15 @@ import compression from '@fastify/compress';
await app.register(compression);
```

By default, `@fastify/compress` will use Brotli compression (on Node >= 11.7.0) when browsers indicate support for the encoding. While Brotli is quite efficient in terms of compression ratio, it's also quite slow. Due to this, you may want to tell fastify-compress to only use deflate and gzip to compress responses; you'll end up with larger responses but they'll be delivered much more quickly.
By default, `@fastify/compress` will use Brotli compression (on Node >= 11.7.0) when browsers indicate support for the encoding. While Brotli can be quite efficient in terms of compression ratio, it can also be quite slow. By default, Brotli sets a maximum compression quality of 11, although it can be adjusted to reduce compression time in lieu of compression quality by adjusting the `BROTLI_PARAM_QUALITY` between 0 min and 11 max. This will require fine tuning to optimize space/time performance. An example with quality 4:

```Typescript
import { constants } from 'zlib';
// somewhere in your initialization file
await app.register(compression, { brotliOptions: { params: { [constants.BROTLI_PARAM_QUALITY]: 4 } } });
```

To simplify, you may want to tell fastify-compress to only use deflate and gzip to compress responses; you'll end up with potentially larger responses but they'll be delivered much more quickly.

To specify encodings, provide a second argument to `app.register`:

Expand Down

0 comments on commit f0622a7

Please sign in to comment.