Express js optimize performance with GZIP Compression

You can improve speed and save bandwidth by using GZIP compression in your express js application. GZIP actually decreases the downloadable amount of data for user request.

Without GZIP compression, when you request a file like, your browser talks to a web server and return plan HTML without compression, if you enable GZIP compression the server will return compressed version of data. Then the browser could download the zipped file, extract it, and then show it to user. The amount of the data that you download for zipped version is lesser then plan version, which means you are saving bandwidth and download time, less user waiting time means faster page loading.

Express js optimize performance with GZIP Compression

Express Js – GZIP compression

You can use the compression middleware to enable GZIP which will support deflate,gzip compression schemes. This middleware will compact the JSON response and the entire static files response. First, let’s install it

After installing it, it will be necessary to include its middleware into the server.js or in our middleware list.

you can start user server by running

Now head over to http://localhost:8080, you can see that the page was served gzipped via the Content-Encoding: gzip header. You can verify static assets cache by observing following, for the first time page should have 200 response, for page refresh (ctrl + R) it should have 304 response code, for doing the hard refresh (ctrl + shift + R) it should have 200 response.

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

I am Arjun from Hyderabad (India). I have been working as a software engineer from last 7+ years, and its my passion to learn new things and implement them as a practice. Aside from work, I likes gardening and spending time with pets.