Bigger Picture Of Node Event Loops
Even though you are a beginner for node js you all know that Node.js is single threaded.But we use Node.js for powerful backends and we can include concurrence functionality into a Node.js application using events and call backs.In simple terms 'Event Loop' is what permits Node.js to perform non blocking I/O operations.Node.js uses event in large scale and it is the reason behind the speed when comparing to other technologies in the same stack
What exactly is 'Event Loop' ? Everything happens in Node.js platform is because of events.'Event Loop' mechanism is inside the library called 'Libuv'.Since there is only one thread both userland code (code of the user) and event loop run in that same thread.Every callback is handled by the so called mechanism 'Event Loop'.Every userland code running in a Node application is a callback.Event Loop is behind the huge success of Node.js development.As I mentioned earlier every Node application is single threaded and what we should be careful when writing the application is not to block the thread.Most of the browsers now have a Event loop for each and every browser tab.It will isolate each and every process and it will stop crashing when a application is executing an infinite loop or heavy processing.
Now I am going to dive into the process inside the Event Loop.When a Node applications starts it will automatically initialise an Event Loop.We can identify six phases inside the process of an Event Loop.
- Timers - Execute Callbacks scheduled by setTimeout() and setInterval()
- I / O Callbacks - Execute I/O Callbacks
- Idle, Prepare - This is the only internal phase
- Poll - Retrieve new I/O events and execute I/O related Callbacks
- Check - Invoke setImmediate() Callbacks
- Close - Invoke close Callbacks
In here I tried to convey the importance of Event Loop and give you a brief idea about it.The post below is about common misconceptions related to Event Loop.
Thank you for being with D Guides.Be safe until we meet again.
Comments
Post a Comment