Share this article Return
As these days most of the companies prefer to hold a grip in all kind of market including that of IoS and Android, they are in continuous need of applications which can run smoothly and successfully on all platforms and we have node js the perfect solution for the server-side development.
Node.JS has been referred to as one of the most reliable and stable means of Java-script based open source Back-end web application framework and one of the world’s most popular programming language.
Clearly, this was not acceptable so we set about doing everything we could think of to make things faster.
So, what could be the reason?
Sometimes node.js?based systems underperformance or even crashes because of few vulnerabilities and weak spots which renders application performance.Problems arise more frequently when a Node.js?based web application experiences rapid traffic fluctuations.
To get the most out of Node.js, you need to cache static content, to proxy and load balance among multiple application servers, and to manage port contention between clients, Node.js, and helpers, such as servers running Socket.IO. NGINX can be used for all of these purposes, making it a great tool for Node.js performance tuning.
This article presents some tips that are known to speed up your Node-based web application development tremendously. So, let’s see each of them one by one.
1. Run in Parallel:
In order to render the HTML page for any dashboard, the node.js application needs to retrieve a lot of data for the dashboard. We need to make multiple internal API calls to fetch various data. Think about a user dashboard. While rendering the dashboard you may execute the following hypothetical calls: In order to render the HTML page for any dashboard, the node.js application needs to retrieve a lot of data for the dashboard. We need to make multiple internal API calls to fetch various data. Think about a user dashboard. While rendering the dashboard you may execute the following hypothetical calls:
The user profile – getUserProfile().
The site list – getSiteList().
Subscriptions – getSubscriptions().
currnet site – getCurrentSite().
Notifications – getNotifications().
At a minimum this means it needs to retrieve the data from the user’s current browsing session to check they’re logged in and it needs to pull in data about the user and about the site in question for the dashboard.In order to retrieve this data, the application needed to make several calls to internal API functions, many of which could take up to 2 seconds to complete. Each request was made by a separate Express middleware, which meant they were running in series. Each request would wait for the previous one to complete before starting.
Since node.js is perfectly suited to running multiple asynchronous functions in parallel, and since a lot of these internal API requests didn’t depend on each other, it made sense to parallelize them — fire off all the requests at once and then continue once they’ve all completed. We achieved this with the aid of the (incredibly useful) async module:
we could do something like this:
2. Cache, Cache, Cache
Unlike most server-based applications, Node.js apps run permanently. You can 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.
3. Enable GZIP
Turning on gzip compression can hugely impact the performance of your web app. When a gzip compatible browser requests for some resource, the server can compress the response before sending it to the browser. If you don’t use gzip for compressing your static resource it might take longer for the browser to fetch it.
In an Express app, you can use the built-in express.static() middleware to serve static content. Additionally, you can use the middleware compression to compress and serve the static content. Here, is a snippet that shows how to do it:
4. Use Client Side Rendering When Possible
With the emergence of many powerful client-side MVC/MVVM frameworks like AngularJS, Ember, Meteor, etc., it has become very easy to create single page apps. Basically, instead of rendering on the server side you will just expose APIs that send JSON responses to the client. On the client side, you can use a framework to consume the JSON and display on the UI. Sending JSON from the server can save bandwidth and thus improve speed because you don’t send layout markup with each request. Rather you just send plain JSON which is then rendered on the client side.
5. Use nginx in Front of Node
Nginx is a tiny and lightweight web server that can be used to reduce the load on your Node.js server. Instead of serving static files from Node, you can configure nginx to serve static content. You can also set up nginx to compress the response using gzip so that the overall response size is small. So, if you are running a production app you might want to use nginx to improve the speed.
This blog post describes some of the most important performance improvements you can make in your Node.js application development. Even after all these optimizations and tweaks, we’re well aware that there’s still plenty of room for improvement. Especially on mobile, where CPU power, memory, rendering performance, latency and bandwidth are all significantly more limited than they are on the desktop.
Also refer our blog on Node.js Version 8 - New Features And Improvements