Supercharge your API with Compression

Performance is critical to any API, taking the time to reduce API response times to be as low as possible is absolutely worth the effort.

Take a look at this example API request in Solaris:

The API response is 44.6kB with a time of 584ms. This isn’…


This content originally appeared on DEV Community and was authored by Mike Eason

Performance is critical to any API, taking the time to reduce API response times to be as low as possible is absolutely worth the effort.

Take a look at this example API request in Solaris:

An API Response

The API response is 44.6kB with a time of 584ms. This isn't terrible but could be smaller and faster with compression.

Express Compression

If you're familiar with Node.js you have probably used Express.js at some time or another.

Express is extensible and has a large number of middleware libraries that can be bolted on. One such library is compression.

To install compression:

npm install compression

Then its a case of simply calling the Express use function to register the middleware like this:

const express = require('express');
const compression = require('compression');
const app = express();

...

// Compress all responses
app.use(compression({
    threshold: 0 // Byte threshold (0 means compress everything)
}));

...

Easy, right? Now calling the same API endpoint we get this:

The same API response, but smaller and faster

The response is now 8.1kB and a time of 101ms, that's over 5x faster than before!

Compressing Specific Responses

With the above code, we'll be compressing all responses, if for some reason you'd like to not compress a response from the API then we can override the filter function like this:

app.use(compression({
    threshold: 0,
    filter: (req, res) => {
        if (req.headers['x-no-compression']) {
            // don't compress responses if this request header is present
            return false;
        }

        // fallback to standard compression
        return compression.filter(req, res);
    }
}));

Any API request with the x-no-compression header will be ignored.

And that's it, your API will now serve compressed responses and should now be performing even better than before!

In my spare time I develop an open source strategy game called **Solaris, check it out.


This content originally appeared on DEV Community and was authored by Mike Eason


Print Share Comment Cite Upload Translate Updates
APA

Mike Eason | Sciencx (2021-04-14T10:12:09+00:00) Supercharge your API with Compression. Retrieved from https://www.scien.cx/2021/04/14/supercharge-your-api-with-compression/

MLA
" » Supercharge your API with Compression." Mike Eason | Sciencx - Wednesday April 14, 2021, https://www.scien.cx/2021/04/14/supercharge-your-api-with-compression/
HARVARD
Mike Eason | Sciencx Wednesday April 14, 2021 » Supercharge your API with Compression., viewed ,<https://www.scien.cx/2021/04/14/supercharge-your-api-with-compression/>
VANCOUVER
Mike Eason | Sciencx - » Supercharge your API with Compression. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/14/supercharge-your-api-with-compression/
CHICAGO
" » Supercharge your API with Compression." Mike Eason | Sciencx - Accessed . https://www.scien.cx/2021/04/14/supercharge-your-api-with-compression/
IEEE
" » Supercharge your API with Compression." Mike Eason | Sciencx [Online]. Available: https://www.scien.cx/2021/04/14/supercharge-your-api-with-compression/. [Accessed: ]
rf:citation
» Supercharge your API with Compression | Mike Eason | Sciencx | https://www.scien.cx/2021/04/14/supercharge-your-api-with-compression/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.