SlideShare a Scribd company logo
Node JS
Understanding the Single Thread Event Loop
Rares Crisan
Lead Educator , BrainStation
About the Speaker
Rares Crisan
Rares Crisan is an experienced developer that has worked with many startup's in Toronto's growing
tech community. He also has a degree in Computer Science from Queen's and studied biomedical
engineering at UBC before completing a law degree at the UBC Peter A Allard Faculty of Law. Rares
like to merge an academic understanding of technology with the real world applications of it.
f
How Servers Work
What We’ll Cover
What is a thread and what is the
difference between a single thread
event loop and multithreading.
Multi Threading vrs
Single Thread
Performance
Gains and Costs
Understanding how server’s handle
large volumes of user requests and
why Node was created to solve this
problem
Node was design to optimally preform
as a web server, however, this does not
make Node suitable for every type of
application.
Web servers preform a very narrow range
of operations. For most servers, the slowest
operations are I/O events.

Servers.
The Web Server
The Web Server
There are many types of servers and they have many different roles in facilitating communication and data transfer. The first HTTP
web server was developed by Tim Berners-Lee in 1989 at CERN for the purpose of posting and sharing scientific information.Web
servers preform many similar operations responding to user data and page requests. A web servers functions may have not vastly
differentiated since its creation, but the volume of requests have increased considerably.
Server I/O
Server I/O
At their core, web servers are simply handling a large stream of short input requests and small output payloads. They do not need to
process complex algorithms or return large volumes of data. Improving the performance of a web server requires increasing the
frequency at which inputs can be processed and outputs can be returned
Some examples of I/O functions are:
Reading/Writing to file.
Database lookups
External Api Requests
A thread of execution is the smallest sequence of
programmed instructions that can be managed
independently by a scheduler

Threads.
Blocking I/O
Blocking I/O
Whenever a process needs to run some operations the instructions are broken down into threads. Threads are short instruction sets
that are run on the CPU. Schedulers are programs built into an OS that prioritize which threads are run and which resources they
require in order to run. Whenever a thread is running and using up a resources the CPU is blocked from handling any more threads
until the thread completes its execution and is switched off the CPU. During this time no other threads can be run on the CPU.
Multithreading
Multithreading
Multithreading is a technique used to split up threads across multiple cores of a CPU so that when one core becomes available the
next available thread in the queue is executed. The output generated by the thread is sent back to the process and then sent back to
wherever the data needs to go. This can speed up thread execution by reducing the amount of time any one core is blocked.
However, on a quad core computer, the most amount of threads that can be processed at a single time is four.
Single Thread Event Loop
The Single Thread Event Loop
Because web servers are designed to handle short, quick, and similar types of request, Node was built to serve those exact needs. It
is equivalent to the idea of pouring a coffee at a coffee shop; it does not take long to pour a single cup of coffee but it can take a long
time to clear a queue if 1000 people show up asking for coffee at once.
Time is lost processing requests when the CPU has to switch from one thread to another; referred to as context switching. The role
of an OS scheduler is to prioritize and move threads on and off the CPU. Node’s event loop relies on the asynchronous features of
JavaScript and utilizes callbacks so the next set of instructions and be added to the single thread after a response is returned from
a worker thread executing the original instructions. Thus, the Node is able to reduce time lost due to context switching.
Multithreading vrs Single
Node Architecture
Node Architecture
T
Node is built upon a multitude of other technologies, languages,
and design paradigms. The reason that Node libraries are built in
JavaScript is because Google’s V8 engine supports
asynchronous JavaScript code. Asynchronous code allows for
callback functions to be stored until needed, Thus, node can
continuously accept inputs and once a response returns, the
callback function can be retrieved from memory and placed on
the call stack to be executed. Ultimately, node is an event-driven
system, which is exactly how the JavaScript language works.
Node Still utilizes OS threads to run tasks such as file I/O or
database queries, however these threads are not part of the
single thread event loop. The completion of these worker threads
are what trigger the callback functions for a particular event
Node Architecture
Node Architecture
T
Node is designed to preform optimally with small
requests, but it’s design can come at a cost.

Performance.
Speed
Speed
T
From the graphs below you can see that Node is able to outperform .NET and PHP, but this trend also extends to
outperforming Rails, Django, and Nginx, for HTTP requests. Node’s speed even allows it to preform well when
handling real time updates of content via sockets.


*Note: The tests below are not perfect but they help give some visual guidelines on Node’s Performance
Source: http://www.salmanq.com/blog/net-and-node-js-performance-comparison/2013/03/ Source: http://www.kogonuso.com/2015/09/comparing-nodejs-vs-php-performance.html
Costs
Costs
T
Node’s biggest shortcoming is it’s inability to handle computationally intensive tasks. If an event running through the
event loop takes a large amount of time to execute (like a sorting a really large array), it will inevitably block the
event loop thread.
Another event that can make Node JS run extremely slow is running it on a server with a single core. If Node is
running on a single core, its single thread blocks other OS threads from operating on the CPU. One personal
example I have preformed was placing a PostGres Database on the same server as NodeJS and utilizing a single
core. Context switching is required to unload the Node Thread and load the DB query thread. The delay is
substantial to say the least.
Questions?
Thank you!
Follow @BrainStation and @rarescrisan on Twitter.
Toronto
445 King St W, #300
Toronto, ON M5V 1K4
Costa Rica
Terracampus Corporativo
Torre 1, Piso 5,
San José Costa Rica
Vancouver
1110 Hamilton St. #410
Vancouver, BC V6B 2S2
LOCATIONS

More Related Content

What's hot

Spring boot introduction
Spring boot introductionSpring boot introduction
Spring boot introductionRasheed Waraich
 
Introduction to Node js
Introduction to Node jsIntroduction to Node js
Introduction to Node jsAkshay Mathur
 
Nodejs Explained with Examples
Nodejs Explained with ExamplesNodejs Explained with Examples
Nodejs Explained with ExamplesGabriele Lana
 
[JS EXPERIENCE 2018] Javascript Event Loop além do setInterval - Derek Stavis
[JS EXPERIENCE 2018] Javascript Event Loop além do setInterval - Derek Stavis[JS EXPERIENCE 2018] Javascript Event Loop além do setInterval - Derek Stavis
[JS EXPERIENCE 2018] Javascript Event Loop além do setInterval - Derek StavisiMasters
 
PUC SE Day 2019 - SpringBoot
PUC SE Day 2019 - SpringBootPUC SE Day 2019 - SpringBoot
PUC SE Day 2019 - SpringBootJosué Neis
 
Spring boot
Spring bootSpring boot
Spring bootsdeeg
 
Concurrency in Java
Concurrency in  JavaConcurrency in  Java
Concurrency in JavaAllan Huang
 
Spring boot Introduction
Spring boot IntroductionSpring boot Introduction
Spring boot IntroductionJeevesh Pandey
 
Introduction to Node JS.pdf
Introduction to Node JS.pdfIntroduction to Node JS.pdf
Introduction to Node JS.pdfBareen Shaikh
 
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...Edureka!
 
JavaScript Interview Questions and Answers | Full Stack Web Development Train...
JavaScript Interview Questions and Answers | Full Stack Web Development Train...JavaScript Interview Questions and Answers | Full Stack Web Development Train...
JavaScript Interview Questions and Answers | Full Stack Web Development Train...Edureka!
 
NodeJS guide for beginners
NodeJS guide for beginnersNodeJS guide for beginners
NodeJS guide for beginnersEnoch Joshua
 
Spring Boot in Action
Spring Boot in Action Spring Boot in Action
Spring Boot in Action Alex Movila
 
Java Concurrency Gotchas
Java Concurrency GotchasJava Concurrency Gotchas
Java Concurrency GotchasAlex Miller
 

What's hot (20)

Introduction to Spring Boot
Introduction to Spring BootIntroduction to Spring Boot
Introduction to Spring Boot
 
Spring boot introduction
Spring boot introductionSpring boot introduction
Spring boot introduction
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Introduction to Node js
Introduction to Node jsIntroduction to Node js
Introduction to Node js
 
Nodejs Explained with Examples
Nodejs Explained with ExamplesNodejs Explained with Examples
Nodejs Explained with Examples
 
[JS EXPERIENCE 2018] Javascript Event Loop além do setInterval - Derek Stavis
[JS EXPERIENCE 2018] Javascript Event Loop além do setInterval - Derek Stavis[JS EXPERIENCE 2018] Javascript Event Loop além do setInterval - Derek Stavis
[JS EXPERIENCE 2018] Javascript Event Loop além do setInterval - Derek Stavis
 
PUC SE Day 2019 - SpringBoot
PUC SE Day 2019 - SpringBootPUC SE Day 2019 - SpringBoot
PUC SE Day 2019 - SpringBoot
 
Spring boot
Spring bootSpring boot
Spring boot
 
Concurrency in Java
Concurrency in  JavaConcurrency in  Java
Concurrency in Java
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Spring boot Introduction
Spring boot IntroductionSpring boot Introduction
Spring boot Introduction
 
Introduction to Node JS.pdf
Introduction to Node JS.pdfIntroduction to Node JS.pdf
Introduction to Node JS.pdf
 
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
 
JavaScript Interview Questions and Answers | Full Stack Web Development Train...
JavaScript Interview Questions and Answers | Full Stack Web Development Train...JavaScript Interview Questions and Answers | Full Stack Web Development Train...
JavaScript Interview Questions and Answers | Full Stack Web Development Train...
 
JS Event Loop
JS Event LoopJS Event Loop
JS Event Loop
 
NodeJS guide for beginners
NodeJS guide for beginnersNodeJS guide for beginners
NodeJS guide for beginners
 
Introduction to spring boot
Introduction to spring bootIntroduction to spring boot
Introduction to spring boot
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Spring Boot in Action
Spring Boot in Action Spring Boot in Action
Spring Boot in Action
 
Java Concurrency Gotchas
Java Concurrency GotchasJava Concurrency Gotchas
Java Concurrency Gotchas
 

Similar to Understanding the Single Thread Event Loop

Operating Systems R20 Unit 2.pptx
Operating Systems R20 Unit 2.pptxOperating Systems R20 Unit 2.pptx
Operating Systems R20 Unit 2.pptxPrudhvi668506
 
Building Applications With the MEAN Stack
Building Applications With the MEAN StackBuilding Applications With the MEAN Stack
Building Applications With the MEAN StackNir Noy
 
Kalp Corporate Node JS Perfect Guide
Kalp Corporate Node JS Perfect GuideKalp Corporate Node JS Perfect Guide
Kalp Corporate Node JS Perfect GuideKalp Corporate
 
Scalability using Node.js
Scalability using Node.jsScalability using Node.js
Scalability using Node.jsratankadam
 
Node.js: A Guided Tour
Node.js: A Guided TourNode.js: A Guided Tour
Node.js: A Guided Tourcacois
 
NodeJS ecosystem
NodeJS ecosystemNodeJS ecosystem
NodeJS ecosystemYukti Kaura
 
Natural Laws of Software Performance
Natural Laws of Software PerformanceNatural Laws of Software Performance
Natural Laws of Software PerformanceGibraltar Software
 
A million connections and beyond - Node.js at scale
A million connections and beyond - Node.js at scaleA million connections and beyond - Node.js at scale
A million connections and beyond - Node.js at scaleTom Croucher
 
Spring Cloud Gateway - Nate Schutta
Spring Cloud Gateway - Nate SchuttaSpring Cloud Gateway - Nate Schutta
Spring Cloud Gateway - Nate SchuttaVMware Tanzu
 
Node.js and the MEAN Stack Building Full-Stack Web Applications.pdf
Node.js and the MEAN Stack Building Full-Stack Web Applications.pdfNode.js and the MEAN Stack Building Full-Stack Web Applications.pdf
Node.js and the MEAN Stack Building Full-Stack Web Applications.pdflubnayasminsebl
 
Net core vs. node.js what to choose when
Net core vs. node.js  what to choose when Net core vs. node.js  what to choose when
Net core vs. node.js what to choose when Katy Slemon
 
Multithreading 101
Multithreading 101Multithreading 101
Multithreading 101Tim Penhey
 

Similar to Understanding the Single Thread Event Loop (20)

Operating Systems R20 Unit 2.pptx
Operating Systems R20 Unit 2.pptxOperating Systems R20 Unit 2.pptx
Operating Systems R20 Unit 2.pptx
 
Building Applications With the MEAN Stack
Building Applications With the MEAN StackBuilding Applications With the MEAN Stack
Building Applications With the MEAN Stack
 
Kalp Corporate Node JS Perfect Guide
Kalp Corporate Node JS Perfect GuideKalp Corporate Node JS Perfect Guide
Kalp Corporate Node JS Perfect Guide
 
Scalability using Node.js
Scalability using Node.jsScalability using Node.js
Scalability using Node.js
 
Node js
Node jsNode js
Node js
 
Node.js: A Guided Tour
Node.js: A Guided TourNode.js: A Guided Tour
Node.js: A Guided Tour
 
NodeJS ecosystem
NodeJS ecosystemNodeJS ecosystem
NodeJS ecosystem
 
Introduction to Node.JS
Introduction to Node.JSIntroduction to Node.JS
Introduction to Node.JS
 
Threads
ThreadsThreads
Threads
 
Best node js course
Best node js courseBest node js course
Best node js course
 
Natural Laws of Software Performance
Natural Laws of Software PerformanceNatural Laws of Software Performance
Natural Laws of Software Performance
 
A million connections and beyond - Node.js at scale
A million connections and beyond - Node.js at scaleA million connections and beyond - Node.js at scale
A million connections and beyond - Node.js at scale
 
Spring Cloud Gateway - Nate Schutta
Spring Cloud Gateway - Nate SchuttaSpring Cloud Gateway - Nate Schutta
Spring Cloud Gateway - Nate Schutta
 
Beginners Node.js
Beginners Node.jsBeginners Node.js
Beginners Node.js
 
Nodejs
NodejsNodejs
Nodejs
 
Node js internal
Node js internalNode js internal
Node js internal
 
Node.js and the MEAN Stack Building Full-Stack Web Applications.pdf
Node.js and the MEAN Stack Building Full-Stack Web Applications.pdfNode.js and the MEAN Stack Building Full-Stack Web Applications.pdf
Node.js and the MEAN Stack Building Full-Stack Web Applications.pdf
 
Net core vs. node.js what to choose when
Net core vs. node.js  what to choose when Net core vs. node.js  what to choose when
Net core vs. node.js what to choose when
 
NodeJS
NodeJSNodeJS
NodeJS
 
Multithreading 101
Multithreading 101Multithreading 101
Multithreading 101
 

More from TorontoNodeJS

Node.js API pitfalls
Node.js API pitfallsNode.js API pitfalls
Node.js API pitfallsTorontoNodeJS
 
Safely Build, Publish & Maintain ES2015, ES2016 Packages Today
Safely Build, Publish & Maintain ES2015, ES2016 Packages TodaySafely Build, Publish & Maintain ES2015, ES2016 Packages Today
Safely Build, Publish & Maintain ES2015, ES2016 Packages TodayTorontoNodeJS
 
Avoiding callback hell with promises
Avoiding callback hell with promisesAvoiding callback hell with promises
Avoiding callback hell with promisesTorontoNodeJS
 
Building your own slack bot on the AWS stack
Building your own slack bot on the AWS stackBuilding your own slack bot on the AWS stack
Building your own slack bot on the AWS stackTorontoNodeJS
 

More from TorontoNodeJS (7)

Node.js API pitfalls
Node.js API pitfallsNode.js API pitfalls
Node.js API pitfalls
 
Safely Build, Publish & Maintain ES2015, ES2016 Packages Today
Safely Build, Publish & Maintain ES2015, ES2016 Packages TodaySafely Build, Publish & Maintain ES2015, ES2016 Packages Today
Safely Build, Publish & Maintain ES2015, ES2016 Packages Today
 
nlp_compromise
nlp_compromisenlp_compromise
nlp_compromise
 
KoNote
KoNoteKoNote
KoNote
 
Avoiding callback hell with promises
Avoiding callback hell with promisesAvoiding callback hell with promises
Avoiding callback hell with promises
 
Node as an API shim
Node as an API shimNode as an API shim
Node as an API shim
 
Building your own slack bot on the AWS stack
Building your own slack bot on the AWS stackBuilding your own slack bot on the AWS stack
Building your own slack bot on the AWS stack
 

Recently uploaded

History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shopHistory+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shoplaozhuseo02
 
Article writing on excessive use of internet.pptx
Article writing on excessive use of internet.pptxArticle writing on excessive use of internet.pptx
Article writing on excessive use of internet.pptxabhinandnam9997
 
The AI Powered Organization-Intro to AI-LAN.pdf
The AI Powered Organization-Intro to AI-LAN.pdfThe AI Powered Organization-Intro to AI-LAN.pdf
The AI Powered Organization-Intro to AI-LAN.pdfSiskaFitrianingrum
 
ER(Entity Relationship) Diagram for online shopping - TAE
ER(Entity Relationship) Diagram for online shopping - TAEER(Entity Relationship) Diagram for online shopping - TAE
ER(Entity Relationship) Diagram for online shopping - TAEHimani415946
 
The+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptxThe+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptxlaozhuseo02
 
How to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptxHow to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptxGal Baras
 
The Use of AI in Indonesia Election 2024: A Case Study
The Use of AI in Indonesia Election 2024: A Case StudyThe Use of AI in Indonesia Election 2024: A Case Study
The Use of AI in Indonesia Election 2024: A Case StudyDamar Juniarto
 
Pvtaan Social media marketing proposal.pdf
Pvtaan Social media marketing proposal.pdfPvtaan Social media marketing proposal.pdf
Pvtaan Social media marketing proposal.pdfPvtaan
 
How Do I Begin the Linksys Velop Setup Process?
How Do I Begin the Linksys Velop Setup Process?How Do I Begin the Linksys Velop Setup Process?
How Do I Begin the Linksys Velop Setup Process?Linksys Velop Login
 
一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理
一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理
一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理aagad
 
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and GuidelinesMulti-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and GuidelinesSanjeev Rampal
 

Recently uploaded (12)

The Best AI Powered Software - Intellivid AI Studio
The Best AI Powered Software - Intellivid AI StudioThe Best AI Powered Software - Intellivid AI Studio
The Best AI Powered Software - Intellivid AI Studio
 
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shopHistory+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
 
Article writing on excessive use of internet.pptx
Article writing on excessive use of internet.pptxArticle writing on excessive use of internet.pptx
Article writing on excessive use of internet.pptx
 
The AI Powered Organization-Intro to AI-LAN.pdf
The AI Powered Organization-Intro to AI-LAN.pdfThe AI Powered Organization-Intro to AI-LAN.pdf
The AI Powered Organization-Intro to AI-LAN.pdf
 
ER(Entity Relationship) Diagram for online shopping - TAE
ER(Entity Relationship) Diagram for online shopping - TAEER(Entity Relationship) Diagram for online shopping - TAE
ER(Entity Relationship) Diagram for online shopping - TAE
 
The+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptxThe+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptx
 
How to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptxHow to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptx
 
The Use of AI in Indonesia Election 2024: A Case Study
The Use of AI in Indonesia Election 2024: A Case StudyThe Use of AI in Indonesia Election 2024: A Case Study
The Use of AI in Indonesia Election 2024: A Case Study
 
Pvtaan Social media marketing proposal.pdf
Pvtaan Social media marketing proposal.pdfPvtaan Social media marketing proposal.pdf
Pvtaan Social media marketing proposal.pdf
 
How Do I Begin the Linksys Velop Setup Process?
How Do I Begin the Linksys Velop Setup Process?How Do I Begin the Linksys Velop Setup Process?
How Do I Begin the Linksys Velop Setup Process?
 
一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理
一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理
一比一原版UTS毕业证悉尼科技大学毕业证成绩单如何办理
 
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and GuidelinesMulti-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
 

Understanding the Single Thread Event Loop

  • 1. Node JS Understanding the Single Thread Event Loop Rares Crisan Lead Educator , BrainStation
  • 2. About the Speaker Rares Crisan Rares Crisan is an experienced developer that has worked with many startup's in Toronto's growing tech community. He also has a degree in Computer Science from Queen's and studied biomedical engineering at UBC before completing a law degree at the UBC Peter A Allard Faculty of Law. Rares like to merge an academic understanding of technology with the real world applications of it.
  • 3. f How Servers Work What We’ll Cover What is a thread and what is the difference between a single thread event loop and multithreading. Multi Threading vrs Single Thread Performance Gains and Costs Understanding how server’s handle large volumes of user requests and why Node was created to solve this problem Node was design to optimally preform as a web server, however, this does not make Node suitable for every type of application.
  • 4. Web servers preform a very narrow range of operations. For most servers, the slowest operations are I/O events.
 Servers.
  • 5. The Web Server The Web Server There are many types of servers and they have many different roles in facilitating communication and data transfer. The first HTTP web server was developed by Tim Berners-Lee in 1989 at CERN for the purpose of posting and sharing scientific information.Web servers preform many similar operations responding to user data and page requests. A web servers functions may have not vastly differentiated since its creation, but the volume of requests have increased considerably.
  • 6. Server I/O Server I/O At their core, web servers are simply handling a large stream of short input requests and small output payloads. They do not need to process complex algorithms or return large volumes of data. Improving the performance of a web server requires increasing the frequency at which inputs can be processed and outputs can be returned Some examples of I/O functions are: Reading/Writing to file. Database lookups External Api Requests
  • 7. A thread of execution is the smallest sequence of programmed instructions that can be managed independently by a scheduler
 Threads.
  • 8. Blocking I/O Blocking I/O Whenever a process needs to run some operations the instructions are broken down into threads. Threads are short instruction sets that are run on the CPU. Schedulers are programs built into an OS that prioritize which threads are run and which resources they require in order to run. Whenever a thread is running and using up a resources the CPU is blocked from handling any more threads until the thread completes its execution and is switched off the CPU. During this time no other threads can be run on the CPU.
  • 9. Multithreading Multithreading Multithreading is a technique used to split up threads across multiple cores of a CPU so that when one core becomes available the next available thread in the queue is executed. The output generated by the thread is sent back to the process and then sent back to wherever the data needs to go. This can speed up thread execution by reducing the amount of time any one core is blocked. However, on a quad core computer, the most amount of threads that can be processed at a single time is four.
  • 10. Single Thread Event Loop The Single Thread Event Loop Because web servers are designed to handle short, quick, and similar types of request, Node was built to serve those exact needs. It is equivalent to the idea of pouring a coffee at a coffee shop; it does not take long to pour a single cup of coffee but it can take a long time to clear a queue if 1000 people show up asking for coffee at once. Time is lost processing requests when the CPU has to switch from one thread to another; referred to as context switching. The role of an OS scheduler is to prioritize and move threads on and off the CPU. Node’s event loop relies on the asynchronous features of JavaScript and utilizes callbacks so the next set of instructions and be added to the single thread after a response is returned from a worker thread executing the original instructions. Thus, the Node is able to reduce time lost due to context switching.
  • 12. Node Architecture Node Architecture T Node is built upon a multitude of other technologies, languages, and design paradigms. The reason that Node libraries are built in JavaScript is because Google’s V8 engine supports asynchronous JavaScript code. Asynchronous code allows for callback functions to be stored until needed, Thus, node can continuously accept inputs and once a response returns, the callback function can be retrieved from memory and placed on the call stack to be executed. Ultimately, node is an event-driven system, which is exactly how the JavaScript language works. Node Still utilizes OS threads to run tasks such as file I/O or database queries, however these threads are not part of the single thread event loop. The completion of these worker threads are what trigger the callback functions for a particular event
  • 14. Node is designed to preform optimally with small requests, but it’s design can come at a cost.
 Performance.
  • 15. Speed Speed T From the graphs below you can see that Node is able to outperform .NET and PHP, but this trend also extends to outperforming Rails, Django, and Nginx, for HTTP requests. Node’s speed even allows it to preform well when handling real time updates of content via sockets. 
 *Note: The tests below are not perfect but they help give some visual guidelines on Node’s Performance Source: http://www.salmanq.com/blog/net-and-node-js-performance-comparison/2013/03/ Source: http://www.kogonuso.com/2015/09/comparing-nodejs-vs-php-performance.html
  • 16. Costs Costs T Node’s biggest shortcoming is it’s inability to handle computationally intensive tasks. If an event running through the event loop takes a large amount of time to execute (like a sorting a really large array), it will inevitably block the event loop thread. Another event that can make Node JS run extremely slow is running it on a server with a single core. If Node is running on a single core, its single thread blocks other OS threads from operating on the CPU. One personal example I have preformed was placing a PostGres Database on the same server as NodeJS and utilizing a single core. Context switching is required to unload the Node Thread and load the DB query thread. The delay is substantial to say the least.
  • 18. Thank you! Follow @BrainStation and @rarescrisan on Twitter. Toronto 445 King St W, #300 Toronto, ON M5V 1K4 Costa Rica Terracampus Corporativo Torre 1, Piso 5, San José Costa Rica Vancouver 1110 Hamilton St. #410 Vancouver, BC V6B 2S2 LOCATIONS