JavaScript - - By Craig Buckler

5 Easy Performance Tweaks for Node.js Express

Node.js has revolutionized web development and the Express framework must share much of the credit. Express may not be the fastest or most advanced server option, but it’s almost certainly the most-used with more than 3 million downloads per month.

If you do nothing, Node.js and Express run blazingly fast. However, there are a number of simple ways to make Express 4.x run even faster…

1. Switch to Production Mode

Express can run in several modes. By default, it presumes development mode which provides exception stack traces and other logging tasks. There is also a debugging mode which logs messages to the console, e.g.

DEBUG=express:* node ./app.js

On your live server, you can noticeably improve performance by switching to production mode. This is achieved by setting the NODE_ENV environment variable to production. It can be set in the current session on Windows prior to launching your Express application:

set NODE_ENV=production

or Mac/Linux:

export NODE_ENV=production

Linux users can also set NODE_ENV in a single line when launching:

NODE_ENV=production node ./app.js

Ideally, you should configure your environment by adding export NODE_ENV=production to your ~/.bash_profile or appropriate start-up script.

2. Enable GZIP

Express 4.x provides minimal functionality which can be enhanced with middleware. One of the less obvious missing features is GZIP compression which shrinks the HTTP payload so it can be expanded by the browser on receipt. To add GZIP, install the compression module using npm:

npm install compression --save

(Depending on your setup, you may need to use sudo on Mac/Linux — there are various ways to fix that.)

In your main application launch file, include the compression module:

var compression = require('compression');

then mount it as the first middleware function (prior to other Express .use methods):

// GZIP all assets

3. Remove Unnecessary Middleware

If you generated a pre-built application using Express Generator or a similar process, remove any middleware modules you’re not using.

For example, if you’re not using cookies you could remove the cookie-parser module or implement a simpler alternative using req.cookies and res.cookie.

It may be possible to remove certain processes such as debugging output in production mode, e.g.

if (app.get('env') !== 'production') {
  // debugging middleware in development only

That said, it’s possibly best to keep serve-favicon. Browsers request favicon.ico files frequently and the module improves caching.

4. Consider a Simpler Template Engine

Jade is the default template engine for many Express installations and it’s a great choice. However, if you’re happy writing your own HTML and just need basic interpolation and evaluation, consider a lightweight alternative such as doT which can be added as Express middleware using the express-dot-engine module.

Alternatively, you can bypass the template engine for simpler responses, e.g.

app.get('/hello', function (req, res) {

Which ever template engine you use, always ensure the view cache is enabled:

app.set('view cache', true);

5. Remember to Cache!

Unlike most server-based applications, Node.js apps run permanently. You can therefore set frequently-used variables such as database connection objects once and re-use them during every request for every user.

The concept can be extended as necessary. In general, the more high-use items you can cache the better performance will be. For example, presume your Express-based CMS application presented links to your five latest articles on the home page. That list could be generated and stored in a variable and only updated when a new article is posted.

Bonus Quick Tips

Want a few more?…

  • Consider using nginx to serve static content and lighten the load on Node.js and Express.
  • Use a build tool such as Gulp to minify and concatenate your HTML templates, CSS and JavaScript files.
  • Limit the use of session variables. User session data is normally stored in memory which can lead to performance issues as usage grows. Consider using an ID only then fetch user information from a database during the request.
  • Increase the maximum number of HTTP sockets from five to a higher number, e.g.
      // use http/https as necessary
      http.globalAgent.maxSockets = 50;
  • Where possible, call sequences of blocking operations in parallel rather than in sequence.
  • Avoid using synchronous blocking calls in everything but application initialization.
  • Always be conscious that code running for one user is blocking code running for every other user.

Do you have further performance tips for Node.js or Express? I’d be glad to hear them in the comments.