From f0622a716681200e4db21e26a6611e1da50fa6b3 Mon Sep 17 00:00:00 2001 From: Eugene Lee Date: Fri, 20 Oct 2023 16:02:19 -0400 Subject: [PATCH 1/3] Docs(/techniques/compression): Brotli compression performance This adds notes on Brotli compression options and provides an example. Closes issue #2834 --- content/techniques/compression.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/content/techniques/compression.md b/content/techniques/compression.md index bb77fea4d3..7b79dec946 100644 --- a/content/techniques/compression.md +++ b/content/techniques/compression.md @@ -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`: From 126705d79049d5d04fbfe50cfa61f88889486b8f Mon Sep 17 00:00:00 2001 From: Eugene Lee <40117342+eugleenyc@users.noreply.github.com> Date: Sat, 21 Oct 2023 07:05:21 -0400 Subject: [PATCH 2/3] Update content/techniques/compression.md Co-authored-by: Antonio Tripodi --- content/techniques/compression.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/techniques/compression.md b/content/techniques/compression.md index 7b79dec946..d8878f2e5c 100644 --- a/content/techniques/compression.md +++ b/content/techniques/compression.md @@ -40,7 +40,7 @@ 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 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 +```typescript import { constants } from 'zlib'; // somewhere in your initialization file await app.register(compression, { brotliOptions: { params: { [constants.BROTLI_PARAM_QUALITY]: 4 } } }); From 723a6fdfda0690337c06381601968553b69a0a4a Mon Sep 17 00:00:00 2001 From: Eugene Lee <40117342+eugleenyc@users.noreply.github.com> Date: Sat, 21 Oct 2023 07:05:28 -0400 Subject: [PATCH 3/3] Update content/techniques/compression.md Co-authored-by: Antonio Tripodi --- content/techniques/compression.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/techniques/compression.md b/content/techniques/compression.md index d8878f2e5c..233c7522a8 100644 --- a/content/techniques/compression.md +++ b/content/techniques/compression.md @@ -46,7 +46,7 @@ import { constants } from 'zlib'; 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 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`: