Blazing fast node.js: 10 performance tips

Blazing fast node.js: 10 performance tips

1. Avoid synchronous code

// Good: write files asynchronously
fs.writeFile(‘message.txt’, ‘Hello Node’, function (err) {
  console.log(“It’s saved and the server remains responsive!”);
// BAD: write files synchronously
fs.writeFileSync(‘message.txt’, ‘Hello Node’);
console.log(“It’s saved, but you just blocked ALL requests!”);

2. Turn off socket pooling

The Node.js http client automatically uses socket pooling: by default, this limits you to 5 sockets per host. While the socket reuse may keep resource growth under control, it will be a serious bottleneck if you need to handle many concurrent requests that all need data from the same host. In these scenarios, it’s a good idea to increase maxSockets or entirely disable socket pooling:

// Disable socket pooling
var http = require(‘http’);
var options = {…..};
options.agent = false;
var req = http.request(options)
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s