Any tips/tricks/suggestions? I heard many good things about Node.js and finally gave it a try. I would like to know about the tricks that are known to improve performance of a Node.js web app.
Here are a few things that worked for me :
Use a library like async to run tasks in parallel. Don't run functions in series if they don't depend on each other. Try to parallelise tasks as much as possible.
Don't use the sync version of many functions unless you really really need to. For example, fs module exposes two functions for reading files :
readFileSync. As Node.js is single threaded and event driven by nature, synchronous operations can block the main thread which in turn may affect the performance of your app.
Use Redis/Memcached for caching content that doesn't change frequently. This will dramatically improve your performance.
Turn on Gzip compression. Today most of the browsers support gzip and there is no reason not to use it. Use a middleware like compression to enable gzip in your Express app.
Utilise client side rendering. It's even better if you support both client and server side rendering. Use a library like React for building isomorphic apps.
Don't store too much data in session (e.g. just store userId for logged-in users) and use Redis/Mongo store for storing session data.
Use nginx as a reverse proxy. Let it serve static resources and dispatch other requests to your Node.js app.
Here is a detailed article that I wrote on SitePoint some days back. It explains all of the points in detail.
The Dev Community
Free, friendly and inclusive platform for developers
Or connect with