Coding techniques in node.js for better performance
Posted By : RAJAT KATIYAR | 17-Apr-2016
- The synchronous code should be avoided.
The node.js allows the execution of the code on a single thread, and that thread handles many concurrent requests. So, if one operation is syncrhronous and blocks others making them wait would not help to get a good performance. Although, node.js allows to make the calls whcich are synchronous.
For example, the calls which include read/ write.
- The node.js shouldn't be used for the assets which are static.
The node.js should not be used for the assets which are static like the images and the CSS. For this purpose a standard webserver is used, for example: nginx. For such purpose the Content Delivery Networks (CDNs) can also be used. This allows to copy the static assets to the various servers which are located in different parts of the world. This helps in reducing the load from the servers of node.js and reduce in the latency by getting delivered the static content.
- The rendering should be done on the client-side.
If the rendering is done on the server side then on every requests,the response is sent along with an HTML page which would not be efficient at all. So the response for the requests sould only contain the dynamic data which going to be changed the other moment.
- If possible, try to go session free
The node.js is mostly used as a part of the MEAN Stack, which also comprises of the Express framework which manages the requests and the response. So, by default the session data is stored in the memory which can lead to some overburden to the server especially when the number of the users grow evidently. So if possible the best choice is to store no data on the server side at all. This will help in increasing the performance.
- The code should be kept small and light.
The small code always helps in better performance. After the completion of the application, it should always be checked that if there is some space for shrinking the code. The smaller and lighter code is generally more efficient and faster in execution.
THANKS
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
RAJAT KATIYAR
Rajat Katiyar is a very bright web app developer, he has good knowledge of JAVA, C/C++, C#.NET. Apart from this he has interest in theater and sports.