Skip to content

Commit

Permalink
Merge pull request #2890 from eugleenyc/compression
Browse files Browse the repository at this point in the history
Docs(/techniques/compression):  Brotli compression performance
  • Loading branch information
kamilmysliwiec authored Oct 23, 2023
2 parents 40dbb2b + 723a6fd commit bcfeae0
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 bcfeae0

Please sign in to comment.