Understanding the Nodejs
Event loop myths and
magic
Saurabh kumar,
nuLL_Programmer
saurav1431@gmail.com
What is Event loop??
But, Before that….
What is Node.JS and does it
Works?
Well,
Node.js is a single threaded,
non-blocking, asynchronous,
event driven, JavaScript runtime
built on V8 engine
index.js
Output:
Call Stack
function foo(b){
var a = 3;
return a* b + 5;
}
function bar(x){
var y = 2;
return foo(x * y);
}
console.log(bar(4));
main();
index.js
Output:
Call Stack
function foo(b){
var a = 3;
return a* b + 5;
}
function bar(x){
var y = 2;
return foo(x * y);
}
console.log(bar(4));
main();
index.js
Output:
Call Stack
function foo(b){
var a = 3;
return a* b + 5;
}
function bar(x){
var y = 2;
return foo(x * y);
}
console.log(bar(4));
main();
console.log(bar(4));
index.js
Output:
Call Stack
function foo(b){
var a = 3;
return a* b + 5;
}
function bar(x){
var y = 2;
return foo(x * y);
}
console.log(bar(4));
main();
console.log(bar(4));
bar(4)
index.js
Output:
Call Stack
function foo(b){
var a = 3;
return a* b + 5;
}
function bar(x){
var y = 2;
return foo(x * y);
}
console.log(bar(4));
main();
console.log(bar(4));
bar(4)
foo(8)
index.js
Output:
Call Stack
function foo(b){
var a = 3;
return a* b + 5;
}
function bar(x){
var y = 2;
return foo(x * y);
}
console.log(bar(4));
main();
console.log(bar(4));
bar(4)
index.js
Output:
Call Stack
function foo(b){
var a = 3;
return a* b + 5;
}
function bar(x){
var y = 2;
return foo(x * y);
}
console.log(bar(4));
main();
console.log(bar(4));
index.js
Output:
Call Stack
function foo(b){
var a = 3;
return a* b + 5;
}
function bar(x){
var y = 2;
return foo(x * y);
}
console.log(bar(4));
main();
What is
Asynchronous
in Node.Js
Example:
console.log(“Hello”);
setTimeout( function() {
console.log(“geekle.us”);
}, 3000);
console.log(“Good Morning”);
Stack
Example:
console.log(“Hello”);
setTimeout( function() {
console.log(“geekle.us”);
}, 3000);
console.log(“Good Morning”);
Stack
main();
main();
console.log(“Hello”);
Example:
console.log(“Hello”);
setTimeout( function() {
console.log(“geekle.us”);
}, 3000);
console.log(“Good Morning”);
Stack
main();
main();
console.log(“Good Morning”);
Example:
console.log(“Hello”);
setTimeout( function() {
console.log(“geekle.us”);
}, 3000);
console.log(“Good Morning”);
Stack
main();
main();
Example:
console.log(“Hello”);
setTimeout( function() {
console.log(“geekle.us”);
}, 3000);
console.log(“Good Morning”);
Stack
main();
main();
console.log(“geekle.us”);
Event loop is in picture
Event loop explained
┌───────────────────────────┐
┌─>│ timers │
│ └─────────────┬─────────────┘
│ ┌─────────────┴─────────────┐
│ │ pending callbacks │
│ └─────────────┬─────────────┘
│ ┌─────────────┴─────────────┐
│ │ idle, prepare │
│ └─────────────┬─────────────┘ ┌───────────────┐
│ ┌─────────────┴─────────────┐ │ incoming: │
│ │ poll │<─────┤ connections, │
│ └─────────────┬─────────────┘ │ data, etc. │
│ ┌─────────────┴─────────────┐ └───────────────┘
│ │ check │
│ └─────────────┬─────────────┘
│ ┌─────────────┴─────────────┐
└──┤ close callbacks │
└───────────────────────────┘
SetImmediate() vs SetTimeout()
setImmediate() is designed to execute a script once the
current poll phase completes.
SetTimeout() schedules a script to be run after a minimum
threshold has elapsed
Process.NextTick()
process.nextTick(() => {
console.log(“execute in next iteration.”);
})
console.log(“execute in current iteration.”);
Output:
execute in current iteration.
execute in next iteration.
When to use Process.nextTick()
1. To allow users to handle errors.
2. Run a request before starting the next iteration of event loop, or the other
event loop continues.
3. Clean unwanted resources.
4. Allow callback to run after call stack and before next iteration of event loop.
Misconception of NodeJS
Event loop
Misconception 1: Everything that’s asynchronous like talking with database, working with
filesystems is handled by a thread pool
Reality: Libuv by default creates a thread poll with multiple thread to offload
asynchronous work.
Today’s operating system already provide asynchronous interfaces for many I/O
tasks. Whenever possible libuv will use those interfaces avoiding usage of thread
pool.
But if there is no other option the thread pool will be used for asynchronous I/O/
Misconception 2: EventEmitter and Event loop are related.
NodeJs event loop is the heart of Nodejs which provide non-blocking and
asynchronous behaviour of an application, while eventemitter is extensively used
when writing the NodeJs application.
That’s All
Thanks
Saurabh kumar,
https://twitter.com/nuLL_Programmer
http://nullprogrammer.github.io/
saurav1431@gmail.com

Understanding the nodejs event loop