Node.js Performance Tips
June 6, 2017
Node.js performance | Quick tips and tricks
Below are some quick tips and tricks for keeping your Node.js app fast.
Avoid Synchronous Activity
While Node.js is technically single-threaded and "asynchronous" in nature (how it handles blocking I/O, etc.), it's possible to still include synchronous operations unintentionally. For example, many developers don't realize that console.log is a synchronous operation when the destination is a terminal or file. Additionally, external libraries and modules may include synchronous code that you aren't aware of. Synchronous operations can silently kill performance if not closely monitored. Always be sure to check your code (and the libraries your code relies on) for synchronous operations that can slow your app down.
Using CDNs to deliver static assets (images, css/js files, etc.) can drastically improve performance. While Node.js is fast, it's not ideal for rendering static assets which can just as easily be hosted and served somewhere else. Not only do CDNs save you performance from a CPU standpoint, they also deliver assets physically closer to your clients. This can drastically improve response time and makes CDNs a no brainer for improved performance.
GZip compression can be used with your http requests to reduce response body size and ultimately speed things up. It's extremely easy to implement with the Express framework. Several compression modules already exist and can easily be incorporated with your project by using app.use().
Watch Your Sessions
Express offers some out-of-the-box functionality for session management. Specifically, Express uses MemoryStore to store session information via RAM. While this is a super convenient way to get going with the Express session object, it won't scale past a single process. As your app grows and more sessions are introduced, you will quickly hit walls with performance.
Express is quick to point this out in their own documentation. While they recommend alternatives including Redis and MongoStore, it's important to understand what your getting yourself into with each of these options before you jump in. When exploring more "production appropriate" alternatives, be sure to understand the nuts and bolts behind how session management is affecting your application's performance.
Client Side Rendering
Rendering your HTML client side is a huge step towards better performance. Whether or not client side rendering is appropriate for your project is an entirely different conversation. Please see our discussion on client vs. server side rendering for an in-depth look at the best option for you.
Use Clusters and Parallel Operations
Although it's single-threaded, Node can take advantage of multiple processors via Node clusters. Modules exist to easily allow your Node instance to take advantage of separate processes which can share server ports, etc. This may not seem necessary when your just getting started, but using Node clusters will allow you to quickly scale your operations as traffic grows.
We've only briefly touched on the power of parallel processing and clusters, however the issues explored above offer some quick tips and tricks for improving performance with Node.js and Express.