Home » Node.js

Node.js - Optimal Performance Part -1

Want to increase the performance of your server quickly? without changing the core logic? Read on.
Submitted by Manu Jemini, on December 15, 2017

The Conditions of development is very different. During development, the requested pressure is minimal and don’t test the capacity of the server or API. In fact, you would never tend to think about the size of the response body. Yes, you read correct, we can easily compress the size of response body which will enhance the performance but your logic will remain the same.

Give it a thought the overall response is an object so to say. This should be compressed as early as you can afford. There are lots of ways to do that but I have an easy middleware to have the fruits of performance easily.

First Step: npm install compression

Second Step: use it on your server

var compression = require('compression');

var express = require('express');

var app = express();

app.use(compression());

.........
........

You know the whole story after that. Doing this not only enhance the possibility of fast response time but also keep the server free.

Don’t use synchronous functions

Synchronous functions and methods tie up the executing process until they return. A single call to asynchronous function might return in a few microseconds or milliseconds, however, in high-traffic websites, these calls add up and reduce the performance of the app. Avoid their use in production.

Although Node and many modules provide synchronous and asynchronous versions of their functions, always use the asynchronous version in production. The only time when an asynchronous function can be justified is upon initial startup.

This will go very long hence I am giving you some very simple example to enhance the performance.

Don’t use console.log()

For beginners, it may be a little strange to not to use something which in-fact is been used so long. Well, I am talking about optimal performance; this will come by lots of research and should be updated.

Console.log() is synchronous. Means the whole server have to wait while you are logging something at your command prompt.

See in future you might want to log in the file of a remote system. Making sense? Let’s say your server is running on the machine 1 and you want the response on the machine 2. Let’s think more maybe you want to save it for the future reference. This is very much possible.

See in future you might want to log in the file of a remote system. Making sense? Let’s say your server is running on the machine 1 and you want the response on the machine 2. Let’s think more maybe you want to save it for the future reference. This is very much possible.

Another reason is that if 10,000 users at one stroke making a request and your server is responding to all of them, it will be a lot better if you won’t use this method.

So what to do? There are lots of third-party libraries which provide asynchronous logging. I am giving you a great library but you may find your own.

winston

This is done it for you.

First step: npm install --save winston

Second step: include it on your server, var winston = require('winston');

Third step: Do logging, winston.info('Hello again distributed logs');

Full Example:

var express = require('express');
var app = express();
var log = require('winston')
app.get('/', function(req, res, next) {
  log.log('info', 'Hello distributed log files!');
  log.info('Hello again distributed logs');
  res.send('respond with a resource');
});
app.listen(3000);
optimal perfomance part-1




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.