We use cookies to create a better browsing experience. By using this website you consent to our cookies policy.

Accept

HOW TO MAKE A NODE.JS APP QUICK?

By ADMIN| May 23, 2018

CATEGORY : BLOG | BLOG DYNAMIC | DEV

TAGS :

Node.JS is considered to be 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. Earlier loading any dashboard would require at least 30 seconds because it consistof HTML pages which itself takes a min of 10 secs, then the application would request several other JavaScript and CSS files, each with a response time averaging 5 seconds. Obviously, this was not acceptable so we some other steps were taken to make things faster.So, what could be the reason? Sometimes performance of node.js based systems is poor or even crashes because of few vulnerabilities and weak spots which affects the application performance. Problems increases more frequently when a Node.js based web application experiences rapid traffic fluctuations.In fact, Node.js is a great tool for creating and running application logic that produces the core, variable content for your web page. But it is not good for serving static content such as images and JavaScript files or load balancing across multiple servers. To avoid these kind of troubles and make the most usage of node.js, it is necessary 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 is used as a great tool for Node.js performance tuning.This article presents some tips to speed up the Node-based web application development. So, let’s see each of them.
  • Run in Parallel:
The node.js application needs to retrieve a lot of data for a dashboard to render its HTML page. Here multiple internal API calls need to be made to fetch various data.So this means it needs to retrieve the data from the user’s current browsing session to check whether the user is logged in or not and it needs to pull in data about the user and about the site regarding the dashboard. In order to retrieve this data, the application needs to make several calls to internal API functions, most of them would take up 2 seconds to complete. Each request would be running in series which means each request would wait for the previous one to complete before starting.Since node.js is perfect for running multiple asynchronous functions in parallel, and since these internal API requests do not depend on each other, it is a good idea to make them parallel. This could be done with the help of async module:
  • Using Cache
Node.js apps run permanently unlike most server-based applications. You can set frequently-used variables such as database connection objects once and it can be re-used during every request for every user. For example, lets see the following snippet which fetches the latest posts to display on a view.You can cache the posts array and clear the cache after an interval. The redis module can be used here. For that Redis needs to be installed on your server. Then you can use a client called node_redis to store key/value pairs. The following snippet shows how we can cache the posts:
  • 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 gzip is not used for compressing the 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 an example showing how to do it.
  • Use Client Side Rendering When Possible
With the introduction of many powerful client-side MVC/MVVM frameworks such as AngularJS, Ember, Meteor, etc., it became easier to create single page apps. Basically, instead of rendering on the server side the APIs need to be exposed 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. While sending JSON from the server, bandwidth can be saved, thus improving the speed because you don’t have to send the layout markup with each request. Only plain JSON needs to be send, which is then rendered on the client side.
  • Use nginx in Front of Node
Nginx is a tiny and lightweight web server that can be used to reduce the load on the Node.js server. Instead of serving static files from Node, the nginx can be configured to serve static content. The nginx can also be set up to compress the response with the help of 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.
  • Reduce and Concatenate JavaScript
The web app speed can be tremendously increased by reducing and concatenating multiple JS files into one. When the browser encounters an  <script> element the page rendering is blocked until the script is fetched and executed (unless async attribute is set). For example, if your page includes five JavaScript files, the browser will make five separate HTTP requests to fetch those. The overall performance can be hugely improved by reducing and concatenating those five files into one. The same applies to CSS files as well.

Have a
question?

Our team is here to help

CONTACT US

OPERATIONS OFFICE:

2F-CS, 46 Ansal Plaza, Sector-1
Vaishali, Ghaziabad-201010 (UP), India

CORPORATE OFFICE:

1002,1004 Antriksh Bhawan
22, KG Marg, New Delhi – 110001

MESSAGE US

info@kreatetechnologies.com

+91 11 66666999