question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Default Brotli compression level is too slow

See original GitHub issue

I’ve recently upgraded from 3.1.0 to 5.0.1 and without any other change, I noticed a huge slow down in my application.

Requests that previously took about 50ms suddenly took over 1s to complete. Downgrading to 3.1.0 again solved this issue.

I wonder if this has something to do with the new Brotli support? But since Node 10 is not actually supporting that natively, maybe it should then be disabled there?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:16 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
damianaiamadcommented, Sep 2, 2020

I stand corrected—I’ve discovered that it is in fact possible to set brotli compression level (1-11). A little obscure, but there you are:

        const options: zlib.BrotliOptions = {
            params: {
                [zlib.constants.BROTLI_PARAM_QUALITY]: 7
            }
        }

For koa:

    koa.use(koaCompress({
        br: {
            params: {
                [zlib.constants.BROTLI_PARAM_QUALITY]: 7
            }
        }
    }))

With a 12MB json payload typical for my app:

BROTLI Level 1 Compression 88.7% Time 36ms Level 2 Compression 89.3% Time 72ms Level 3 Compression 89.6% Time 74ms Level 4 Compression 91.1% Time 112ms Level 5 Compression 92.0% Time 180ms Level 6 Compression 92.1% Time 222ms Level 7 Compression 92.2% Time 302ms Level 8 Compression 92.3% Time 397ms Level 9 Compression 92.4% Time 545ms Level 10 Compression 93.1% Time 6441ms Level 11 Compression 93.6% Time 33956ms

GZIP Level 1 Compression 87.4% Time 75ms Level 2 Compression 87.8% Time 79ms Level 3 Compression 88.2% Time 91ms Level 4 Compression 89.1% Time 115ms Level 5 Compression 89.4% Time 135ms Level 6 Compression 89.9% Time 169ms Level 7 Compression 90.0% Time 215ms Level 8 Compression 90.2% Time 505ms Level 9 Compression 90.3% Time 557ms

So this makes things a lot more interesting. For the kind of json data I work with, there seems to be a large sweet spot where brotli has the edge: both faster and smaller file size!

You may need to run some experiments with your own data to tune the compression level (1-11 for brotli, 1-9 for gzip).

2reactions
damianaiamadcommented, Oct 3, 2020

I think koa-compress can use brotli by default so long as it sets the compression level to something suitable for doing realtime compression.

NodeJS default is Brotli L11, which is great for offline compression, but woeful for realtime compression. Since koa-compress is all about realtime compression, it must not use the NodeJS default.

I suggest Brotli L5. From my experiments (above), this works out 3x faster then default GZIP L9 compression. Of course your mileage will vary.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Brotli compression is very slow · Issue #135 - GitHub
Brotli seems to be extremely slow to compress files, ... We need to find out what the 'ideal' level for brotli is and...
Read more >
Setting Brotli compression level when using response ...
Brotli at its highest level (11) is extremely slow compared to Gzip at its highest level (9). Slow compression is fine for static...
Read more >
Improving website performance with brotli - DEV Community ‍ ‍
In this experiment the compression speed of brotli at level 10 is about 100 times slower than gzip at level 6. This characteristic...
Read more >
'You can't use Brotli for dynamic content' - Expedited Security
Mainly because they contain one big fat lie: that Brotli is slower than gzip to compress, so you can't use it for dynamic...
Read more >
Brotli Compression: Making the Big Smaller and Faster (Part 2)
In this post, we are going to talk about the Brotli Compression in comparison to other compression algorithms.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found