Performance Improvement | Node Js | PART 1

Performance Improvement In Node Js


Introduction

     node.js is known for its performance, efficiency, diversity. That's the reason why the node is one of the most popular backend technologies and nowadays every big organization has already shifted to node.js and some are willing to shift. but using efficient technology is not just sufficient to achieve great performance. Although node.js provide a fast experience, programmers need to keep on tracking the performance matrix.
    I have one and a half years of experience in node.js . and I have learned lots of new things during this period. I am gonna share all my knowledge through this article. I hope you will find it informative.
    While working on node.js you need to keep on measuring application performance continuously. because if you think you will measure it later on after the application finished trust me it's gonna be lots of pain for you. especially in production mode because at that time you will be under pressure and you will find yourself working on the same thing repeatedly and you will get frustrated  . to avoid this I will suggest you be smart right at the beginning and consider all performance parameters nicely.

1. Do Not Ever Block Your Event Loop    

        If you don't know what the hell is an event loop let me tell you, the event loop is the heart of node.js where all the most important things happen. Basically, the event loop takes care of all necessary operations which any program demands to execute. event loop allocates the task to worker threads to get all work done and the worker thread responds back with a callback to the main thread once execution is done.

node js event loop image


Event Loop Lifecycle phases
Timer Phase
Pending callbacks
Idle, prepare
Poll
Check
Close callback


2. Do Not Use NPM Packages Unnecessary

        Most new node.js programmers kind of obsessed with exploring new npm packages and using it. but it's not a good habit because let’s try to break it down. before installing any npm package search about your problem and try to write logic on your own. because the “package” name itself represents that bunch of functionality in most cases we only use one or two functions from any package. Which affects badly on performance. In short use of packages means maintaining unnecessary code.

3. Load Balancing 

        The first question that comes to mind is what is load balancing ? . load balancing is a technique used to distribute the workload on machine resources. E.g if you have a processor with 4 core and you run a node application you are expecting this application to respond to the result quickly because you are thinking all 4 core will involve in this code execution but let me tell you that doesn’t happen by default in node.js.We know node js is single-threaded that's the reason no matter how many tasks are there it will get done by single-core itself and the other 3 cores will remain idle. 
  If you want to involve all cores in process execution you need to write separate code for that because by default node.js doesn't distribute work or load balance. Below given some of the ways to deal with load balancing



4)  Use Streams in node JS

    reading any file and writing into the file is a very common task in node js. most developer use fs.readFile or fs.writeFile method to read or write operations of the file but this is a very time-consuming way because when we do any fs.readFile operation then that time file data gets load into buffer entirely and then we can use that buffer . that means we cannot access files data before it read entirely in memory. when the size of the file is hudge then apparently it will take more time to do the further operation and eventually, your application will become slower.

4) Streams with pipelining

Stream pipelining is again one more step you can take to improve memory utilization and you can optimize memory one of the most important thing in any optimization process is its memory pipelining technique helps you to save memory because with this technique you stream the data to read or write operations and this is how we can save up some memory by avoiding loading file in the buffer.

Conclusion 

    This article is just basics about what is actually happening under the hood, before we directly jump into the performance improvement and profiling part these are the terms everyone should know about node js .

Post a Comment

0 Comments