SlideShare a Scribd company logo
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

More Related Content

PPTX
JS Event Loop
PPTX
All you need to know about the JavaScript event loop
PDF
JavaScript Event Loop
PDF
Workshop 4: NodeJS. Express Framework & MongoDB.
PPTX
Spring Security 5
PDF
NodeJS for Beginner
PDF
Railway Oriented Programming
PDF
MongoDB WiredTiger Internals: Journey To Transactions
JS Event Loop
All you need to know about the JavaScript event loop
JavaScript Event Loop
Workshop 4: NodeJS. Express Framework & MongoDB.
Spring Security 5
NodeJS for Beginner
Railway Oriented Programming
MongoDB WiredTiger Internals: Journey To Transactions

What's hot (20)

PDF
Introduction to Celery
PPTX
Asynchronous programming in C#
PPTX
Ndc14 분산 서버 구축의 ABC
PPTX
User controls
PPTX
JavaScript Event Loop
PDF
카카오톡의 서버사이드 코틀린
PDF
Asynchronous API in Java8, how to use CompletableFuture
PDF
Solid NodeJS with TypeScript, Jest & NestJS
PDF
Windows Registered I/O (RIO) vs IOCP
PPTX
A Step Towards Data Orientation
PDF
Nestjs MasterClass Slides
PPTX
Java 8 streams
PDF
[pgday.Seoul 2022] POSTGRES 테스트코드로 기여하기 - 이동욱
PPTX
Introduction to Node js
PPTX
GPGPU(CUDA)를 이용한 MMOG 캐릭터 충돌처리
PDF
Functional Programming Patterns (NDC London 2014)
PDF
덤프 파일을 통한 사후 디버깅 실용 테크닉 NDC2012
PPTX
동기화 시대를 뛰어넘는 비동기 프로그래밍
PDF
MMOG Server-Side 충돌 및 이동처리 설계와 구현
PDF
Advanced Node.JS Meetup
Introduction to Celery
Asynchronous programming in C#
Ndc14 분산 서버 구축의 ABC
User controls
JavaScript Event Loop
카카오톡의 서버사이드 코틀린
Asynchronous API in Java8, how to use CompletableFuture
Solid NodeJS with TypeScript, Jest & NestJS
Windows Registered I/O (RIO) vs IOCP
A Step Towards Data Orientation
Nestjs MasterClass Slides
Java 8 streams
[pgday.Seoul 2022] POSTGRES 테스트코드로 기여하기 - 이동욱
Introduction to Node js
GPGPU(CUDA)를 이용한 MMOG 캐릭터 충돌처리
Functional Programming Patterns (NDC London 2014)
덤프 파일을 통한 사후 디버깅 실용 테크닉 NDC2012
동기화 시대를 뛰어넘는 비동기 프로그래밍
MMOG Server-Side 충돌 및 이동처리 설계와 구현
Advanced Node.JS Meetup
Ad

Similar to Understanding the nodejs event loop (20)

PPTX
A beginner's guide to eventloops in node
PDF
Node.js Event Loop & EventEmitter
PPTX
Events for JavaScript event loop track.pptx
PPTX
Node js for backend server development.
PPTX
Introduction to Node.js
PDF
Node js internal
PPTX
Bucks County Tech Meetup: node.js introduction
PPTX
JavaScript Engines and Event Loop
PDF
Basic Understanding and Implement of Node.js
PDF
Javascript internals
PPT
JavaScript Event Loop
PDF
Deep Dive into Node.js Event Loop.pdf
PDF
Frontend Track NodeJS
ODP
Event Loop in Javascript
PDF
Introduction to Node JS2.pdf
PPTX
Javascript why what and how
PDF
"Leveraging the Event Loop for Blazing-Fast Applications!", Michael Di Prisco
PPTX
How NOT to write in Node.js
PDF
The evolution of asynchronous javascript
PPTX
Introduction to node.js By Ahmed Assaf
A beginner's guide to eventloops in node
Node.js Event Loop & EventEmitter
Events for JavaScript event loop track.pptx
Node js for backend server development.
Introduction to Node.js
Node js internal
Bucks County Tech Meetup: node.js introduction
JavaScript Engines and Event Loop
Basic Understanding and Implement of Node.js
Javascript internals
JavaScript Event Loop
Deep Dive into Node.js Event Loop.pdf
Frontend Track NodeJS
Event Loop in Javascript
Introduction to Node JS2.pdf
Javascript why what and how
"Leveraging the Event Loop for Blazing-Fast Applications!", Michael Di Prisco
How NOT to write in Node.js
The evolution of asynchronous javascript
Introduction to node.js By Ahmed Assaf
Ad

Recently uploaded (20)

PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
UNIT 4 Total Quality Management .pptx
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
Welding lecture in detail for understanding
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PPTX
additive manufacturing of ss316l using mig welding
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
Geodesy 1.pptx...............................................
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPT
Project quality management in manufacturing
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
Sustainable Sites - Green Building Construction
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
Digital Logic Computer Design lecture notes
PDF
PPT on Performance Review to get promotions
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
UNIT 4 Total Quality Management .pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Welding lecture in detail for understanding
Arduino robotics embedded978-1-4302-3184-4.pdf
additive manufacturing of ss316l using mig welding
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Geodesy 1.pptx...............................................
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Strings in CPP - Strings in C++ are sequences of characters used to store and...
Operating System & Kernel Study Guide-1 - converted.pdf
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Project quality management in manufacturing
Foundation to blockchain - A guide to Blockchain Tech
Sustainable Sites - Green Building Construction
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Digital Logic Computer Design lecture notes
PPT on Performance Review to get promotions
Lesson 3_Tessellation.pptx finite Mathematics
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...

Understanding the nodejs event loop