SlideShare a Scribd company logo
The JavaScript Event Loop
Concurrency in the Language of the Web
Me
● Developer at Carbon Five
● Software developer since 2000
● Serious JavaScript since 2006
What’s this presentation about?
● JavaScript’s asynchronous approach to I/O
● Elements of JavaScript runtimes that make
this possible
● Runtime-specific approaches to handling
concurrency
Target Audience
● Anyone interested in JavaScript
● Actually, all web developers
● Anyone interested in different approaches to
tackling concurrency
Assumptions
● You’ve written a fair amount of JavaScript
● You’re familiar with some computer-sciency
concepts like:
○ call stack
○ thread
Quick Definition: Call Stack
● A stack data structure that stores information
about active functions
● So, when f ➝ g ➝ h, you have three frames
on call stack
Quick Definition: Call Stack
function alpha() { beta(); }
function beta() { kappa(); }
function kappa() { console.log('foo'); }
Quick Definition: Thread
● One execution path through your code
JS Guiding Principle: No Waiting
● Your JavaScript code follows a single thread
of execution
● Don’t make the thread wait
● Register a callback function and let the
thread continue on
Synchronous I/O
# strawman Ruby, with Faraday
response = Faraday.get 'http://www.google.com'
puts response
puts 'Done!'
Asynchronous I/O
// strawman JavaScript, with Request
request('http://www.google.com', function(error, response, body) {
console.log(body);
});
console.log('Not yet done!'); // visible before response from Google!
Elements of a JavaScript Runtime
● Message queue
● Event loop
Example: addEventListener
function init() {
var link = document.getElementById("foo");
link.addEventListener("click", function changeColor() {
this.style.color = "burlywood";
});
}
init();
Visual Model: addEventListener
Example: setTimeout
function init() {
var link = document.getElementById("foo");
setTimeout(function changeColor() {
link.style.color = "burlywood";
}, 1000);
}
init();
Visual Model: setTimeout
Why?
● Handle concurrent operations without
multiple threads of execution
● Works great if you’re I/O bound (or waiting
on user events), and maybe not-so-good if
you’re CPU-bound
Synchronous Operations
● Some operations are synchronous
● Event Loop is single-threaded so...
● Expensive, synchronous operations will
cause runtime to become unresponsive
Blocking, Expensive Operation
function expensive(ops) {
var temp = 0;
while(ops--) {
temp += Math.random();
}
};
expensive(1000000000);
HTML5: Unblock w/Web Workers
● Additional thread of execution for offloading
expensive operations
● Separate event loop, message queue, stack
● Communicates with main thread through
messaging
● Supported by all modern browsers + IE10
Unblock w/Web Workers (cont.)
Unblock w/Web Workers (cont.)
// main.js
var worker = new Worker("worker.js");
worker.addEventListener("message", log(e) {
// Receive results from worker thread
console.log("received: " + e.data.toString());
}, false);
// Send child process some work
worker.postMessage(1000000000);
// worker.js
onmessage = function expensive(e) {
var ops = e.data,
temp = 0;
while(ops--) {
temp += Math.random();
}
postMessage(temp);
};
Node.js: Forking Processes
● Use child_process.fork
● Creates a whole new instance of V8
● Built-in communication channel
● Much heavier than a thread (more memory)
Node.js: Forking Processes
// main.js
var cp = require("child_process");
var worker = cp.fork("./worker");
worker.on("message", function(m) {
// Receive results from child process
console.log("received: " + m);
});
// Send child process some work
worker.send("1000000000");
// worker.js
process.on("message", function(m) {
var max = parseInt(m, 10), ops = max,
temp = 0;
while(ops--) {
temp += Math.random();
if (ops % (max/10) === 0) {
process.send(ops + " ops complete");
}
}
});
Takeaways
● All JavaScript runtimes expose a single-
threaded event loop
● Build your system around events and
asynchronous callbacks
● Various runtimes provide additional tools for
handling concurrency
● Just one approach - there are many more!

More Related Content

Similar to The JavaScript Event Loop - Concurrency in the Language of the Web (20)

JS Event Loop
JS Event LoopJS Event Loop
JS Event Loop
Saai Vignesh P
 
The evolution of asynchronous javascript
The evolution of asynchronous javascriptThe evolution of asynchronous javascript
The evolution of asynchronous javascript
Alessandro Cinelli (cirpo)
 
Node.js: A Guided Tour
Node.js: A Guided TourNode.js: A Guided Tour
Node.js: A Guided Tour
cacois
 
Basic Understanding and Implement of Node.js
Basic Understanding and Implement of Node.jsBasic Understanding and Implement of Node.js
Basic Understanding and Implement of Node.js
Gary Yeh
 
Node.js concurrency
Node.js concurrencyNode.js concurrency
Node.js concurrency
Giacomo Fornari
 
Real timeninja - Codemotion 2015 Roma
Real timeninja - Codemotion 2015 RomaReal timeninja - Codemotion 2015 Roma
Real timeninja - Codemotion 2015 Roma
MeanMilan
 
Node js for backend server development.
Node js for backend  server development.Node js for backend  server development.
Node js for backend server development.
digitalindia1231
 
NodeJS Concurrency
NodeJS ConcurrencyNodeJS Concurrency
NodeJS Concurrency
pgriess
 
Frontend Track NodeJS
Frontend Track NodeJSFrontend Track NodeJS
Frontend Track NodeJS
Marcelo Serpa
 
Node.js Workshop - Sela SDP 2015
Node.js Workshop  - Sela SDP 2015Node.js Workshop  - Sela SDP 2015
Node.js Workshop - Sela SDP 2015
Nir Noy
 
JavaScript Async for Effortless UX
JavaScript Async for Effortless UXJavaScript Async for Effortless UX
JavaScript Async for Effortless UX
재석 강
 
Introduction to Node JS.pdf
Introduction to Node JS.pdfIntroduction to Node JS.pdf
Introduction to Node JS.pdf
Bareen Shaikh
 
Event Driven Javascript
Event Driven JavascriptEvent Driven Javascript
Event Driven Javascript
Federico Galassi
 
The art of concurrent programming
The art of concurrent programmingThe art of concurrent programming
The art of concurrent programming
Iskren Chernev
 
Asynchronous development in JavaScript
Asynchronous development  in JavaScriptAsynchronous development  in JavaScript
Asynchronous development in JavaScript
Amitai Barnea
 
Intro to Asynchronous Javascript
Intro to Asynchronous JavascriptIntro to Asynchronous Javascript
Intro to Asynchronous Javascript
Garrett Welson
 
Javascript why what and how
Javascript why what and howJavascript why what and how
Javascript why what and how
sureshpraja1234
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
Richard Lee
 
"Leveraging the Event Loop for Blazing-Fast Applications!", Michael Di Prisco
"Leveraging the Event Loop for Blazing-Fast Applications!",  Michael Di Prisco"Leveraging the Event Loop for Blazing-Fast Applications!",  Michael Di Prisco
"Leveraging the Event Loop for Blazing-Fast Applications!", Michael Di Prisco
Fwdays
 
Playing With Fire - An Introduction to Node.js
Playing With Fire - An Introduction to Node.jsPlaying With Fire - An Introduction to Node.js
Playing With Fire - An Introduction to Node.js
Mike Hagedorn
 
Node.js: A Guided Tour
Node.js: A Guided TourNode.js: A Guided Tour
Node.js: A Guided Tour
cacois
 
Basic Understanding and Implement of Node.js
Basic Understanding and Implement of Node.jsBasic Understanding and Implement of Node.js
Basic Understanding and Implement of Node.js
Gary Yeh
 
Real timeninja - Codemotion 2015 Roma
Real timeninja - Codemotion 2015 RomaReal timeninja - Codemotion 2015 Roma
Real timeninja - Codemotion 2015 Roma
MeanMilan
 
Node js for backend server development.
Node js for backend  server development.Node js for backend  server development.
Node js for backend server development.
digitalindia1231
 
NodeJS Concurrency
NodeJS ConcurrencyNodeJS Concurrency
NodeJS Concurrency
pgriess
 
Frontend Track NodeJS
Frontend Track NodeJSFrontend Track NodeJS
Frontend Track NodeJS
Marcelo Serpa
 
Node.js Workshop - Sela SDP 2015
Node.js Workshop  - Sela SDP 2015Node.js Workshop  - Sela SDP 2015
Node.js Workshop - Sela SDP 2015
Nir Noy
 
JavaScript Async for Effortless UX
JavaScript Async for Effortless UXJavaScript Async for Effortless UX
JavaScript Async for Effortless UX
재석 강
 
Introduction to Node JS.pdf
Introduction to Node JS.pdfIntroduction to Node JS.pdf
Introduction to Node JS.pdf
Bareen Shaikh
 
The art of concurrent programming
The art of concurrent programmingThe art of concurrent programming
The art of concurrent programming
Iskren Chernev
 
Asynchronous development in JavaScript
Asynchronous development  in JavaScriptAsynchronous development  in JavaScript
Asynchronous development in JavaScript
Amitai Barnea
 
Intro to Asynchronous Javascript
Intro to Asynchronous JavascriptIntro to Asynchronous Javascript
Intro to Asynchronous Javascript
Garrett Welson
 
Javascript why what and how
Javascript why what and howJavascript why what and how
Javascript why what and how
sureshpraja1234
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
Richard Lee
 
"Leveraging the Event Loop for Blazing-Fast Applications!", Michael Di Prisco
"Leveraging the Event Loop for Blazing-Fast Applications!",  Michael Di Prisco"Leveraging the Event Loop for Blazing-Fast Applications!",  Michael Di Prisco
"Leveraging the Event Loop for Blazing-Fast Applications!", Michael Di Prisco
Fwdays
 
Playing With Fire - An Introduction to Node.js
Playing With Fire - An Introduction to Node.jsPlaying With Fire - An Introduction to Node.js
Playing With Fire - An Introduction to Node.js
Mike Hagedorn
 

Recently uploaded (20)

WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODSWIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
samueljackson3773
 
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghjfHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
yadavshivank2006
 
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
Journal of Soft Computing in Civil Engineering
 
Computer_vision-photometric_image_formation.pdf
Computer_vision-photometric_image_formation.pdfComputer_vision-photometric_image_formation.pdf
Computer_vision-photometric_image_formation.pdf
kumarprem6767merp
 
SEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair KitSEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair Kit
projectultramechanix
 
FINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptx
FINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptxFINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptx
FINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptx
kippcam
 
Irja Straus - Beyond Pass and Fail - DevTalks.pdf
Irja Straus - Beyond Pass and Fail - DevTalks.pdfIrja Straus - Beyond Pass and Fail - DevTalks.pdf
Irja Straus - Beyond Pass and Fail - DevTalks.pdf
Irja Straus
 
ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025
Rahul
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
djiceramil
 
Présentation_gestion[1] [Autosaved].pptx
Présentation_gestion[1] [Autosaved].pptxPrésentation_gestion[1] [Autosaved].pptx
Présentation_gestion[1] [Autosaved].pptx
KHADIJAESSAKET
 
22PCOAM16 _ML_Unit 3 Notes & Question bank
22PCOAM16 _ML_Unit 3 Notes & Question bank22PCOAM16 _ML_Unit 3 Notes & Question bank
22PCOAM16 _ML_Unit 3 Notes & Question bank
Guru Nanak Technical Institutions
 
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdfRearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Takumi Amitani
 
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Rigor, ethics, wellbeing and resilience in the ICT doctoral journeyRigor, ethics, wellbeing and resilience in the ICT doctoral journey
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Yannis
 
Airport_Substation_With_Diagrams (2).pptx
Airport_Substation_With_Diagrams (2).pptxAirport_Substation_With_Diagrams (2).pptx
Airport_Substation_With_Diagrams (2).pptx
BibekMedhi2
 
社内勉強会資料_Chain of Thought .
社内勉強会資料_Chain of Thought                           .社内勉強会資料_Chain of Thought                           .
社内勉強会資料_Chain of Thought .
NABLAS株式会社
 
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) ProjectMontreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Alexandra N. Martinez
 
Structure of OS ppt Structure of OsS ppt
Structure of OS ppt Structure of OsS pptStructure of OS ppt Structure of OsS ppt
Structure of OS ppt Structure of OsS ppt
Wahajch
 
Impurities of Water and their Significance.pptx
Impurities of Water and their Significance.pptxImpurities of Water and their Significance.pptx
Impurities of Water and their Significance.pptx
dhanashree78
 
Ppt on the related on the solar power system for electric vehicle engineering
Ppt on the related on the solar power system for electric vehicle engineeringPpt on the related on the solar power system for electric vehicle engineering
Ppt on the related on the solar power system for electric vehicle engineering
ravindrabodke
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODSWIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
WIRELESS COMMUNICATION SECURITY AND IT’S PROTECTION METHODS
samueljackson3773
 
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghjfHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
yadavshivank2006
 
Computer_vision-photometric_image_formation.pdf
Computer_vision-photometric_image_formation.pdfComputer_vision-photometric_image_formation.pdf
Computer_vision-photometric_image_formation.pdf
kumarprem6767merp
 
SEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair KitSEW make Brake BE05 – BE30 Brake – Repair Kit
SEW make Brake BE05 – BE30 Brake – Repair Kit
projectultramechanix
 
FINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptx
FINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptxFINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptx
FINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptx
kippcam
 
Irja Straus - Beyond Pass and Fail - DevTalks.pdf
Irja Straus - Beyond Pass and Fail - DevTalks.pdfIrja Straus - Beyond Pass and Fail - DevTalks.pdf
Irja Straus - Beyond Pass and Fail - DevTalks.pdf
Irja Straus
 
ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025
Rahul
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
362 Alec Data Center Solutions-Slysium Data Center-AUH-Glands & Lugs, Simplex...
djiceramil
 
Présentation_gestion[1] [Autosaved].pptx
Présentation_gestion[1] [Autosaved].pptxPrésentation_gestion[1] [Autosaved].pptx
Présentation_gestion[1] [Autosaved].pptx
KHADIJAESSAKET
 
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdfRearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Takumi Amitani
 
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Rigor, ethics, wellbeing and resilience in the ICT doctoral journeyRigor, ethics, wellbeing and resilience in the ICT doctoral journey
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Yannis
 
Airport_Substation_With_Diagrams (2).pptx
Airport_Substation_With_Diagrams (2).pptxAirport_Substation_With_Diagrams (2).pptx
Airport_Substation_With_Diagrams (2).pptx
BibekMedhi2
 
社内勉強会資料_Chain of Thought .
社内勉強会資料_Chain of Thought                           .社内勉強会資料_Chain of Thought                           .
社内勉強会資料_Chain of Thought .
NABLAS株式会社
 
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) ProjectMontreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Alexandra N. Martinez
 
Structure of OS ppt Structure of OsS ppt
Structure of OS ppt Structure of OsS pptStructure of OS ppt Structure of OsS ppt
Structure of OS ppt Structure of OsS ppt
Wahajch
 
Impurities of Water and their Significance.pptx
Impurities of Water and their Significance.pptxImpurities of Water and their Significance.pptx
Impurities of Water and their Significance.pptx
dhanashree78
 
Ppt on the related on the solar power system for electric vehicle engineering
Ppt on the related on the solar power system for electric vehicle engineeringPpt on the related on the solar power system for electric vehicle engineering
Ppt on the related on the solar power system for electric vehicle engineering
ravindrabodke
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
Ad

The JavaScript Event Loop - Concurrency in the Language of the Web

  • 1. The JavaScript Event Loop Concurrency in the Language of the Web
  • 2. Me ● Developer at Carbon Five ● Software developer since 2000 ● Serious JavaScript since 2006
  • 3. What’s this presentation about? ● JavaScript’s asynchronous approach to I/O ● Elements of JavaScript runtimes that make this possible ● Runtime-specific approaches to handling concurrency
  • 4. Target Audience ● Anyone interested in JavaScript ● Actually, all web developers ● Anyone interested in different approaches to tackling concurrency
  • 5. Assumptions ● You’ve written a fair amount of JavaScript ● You’re familiar with some computer-sciency concepts like: ○ call stack ○ thread
  • 6. Quick Definition: Call Stack ● A stack data structure that stores information about active functions ● So, when f ➝ g ➝ h, you have three frames on call stack
  • 7. Quick Definition: Call Stack function alpha() { beta(); } function beta() { kappa(); } function kappa() { console.log('foo'); }
  • 8. Quick Definition: Thread ● One execution path through your code
  • 9. JS Guiding Principle: No Waiting ● Your JavaScript code follows a single thread of execution ● Don’t make the thread wait ● Register a callback function and let the thread continue on
  • 10. Synchronous I/O # strawman Ruby, with Faraday response = Faraday.get 'http://www.google.com' puts response puts 'Done!'
  • 11. Asynchronous I/O // strawman JavaScript, with Request request('http://www.google.com', function(error, response, body) { console.log(body); }); console.log('Not yet done!'); // visible before response from Google!
  • 12. Elements of a JavaScript Runtime ● Message queue ● Event loop
  • 13. Example: addEventListener function init() { var link = document.getElementById("foo"); link.addEventListener("click", function changeColor() { this.style.color = "burlywood"; }); } init();
  • 15. Example: setTimeout function init() { var link = document.getElementById("foo"); setTimeout(function changeColor() { link.style.color = "burlywood"; }, 1000); } init();
  • 17. Why? ● Handle concurrent operations without multiple threads of execution ● Works great if you’re I/O bound (or waiting on user events), and maybe not-so-good if you’re CPU-bound
  • 18. Synchronous Operations ● Some operations are synchronous ● Event Loop is single-threaded so... ● Expensive, synchronous operations will cause runtime to become unresponsive
  • 19. Blocking, Expensive Operation function expensive(ops) { var temp = 0; while(ops--) { temp += Math.random(); } }; expensive(1000000000);
  • 20. HTML5: Unblock w/Web Workers ● Additional thread of execution for offloading expensive operations ● Separate event loop, message queue, stack ● Communicates with main thread through messaging ● Supported by all modern browsers + IE10
  • 22. Unblock w/Web Workers (cont.) // main.js var worker = new Worker("worker.js"); worker.addEventListener("message", log(e) { // Receive results from worker thread console.log("received: " + e.data.toString()); }, false); // Send child process some work worker.postMessage(1000000000); // worker.js onmessage = function expensive(e) { var ops = e.data, temp = 0; while(ops--) { temp += Math.random(); } postMessage(temp); };
  • 23. Node.js: Forking Processes ● Use child_process.fork ● Creates a whole new instance of V8 ● Built-in communication channel ● Much heavier than a thread (more memory)
  • 24. Node.js: Forking Processes // main.js var cp = require("child_process"); var worker = cp.fork("./worker"); worker.on("message", function(m) { // Receive results from child process console.log("received: " + m); }); // Send child process some work worker.send("1000000000"); // worker.js process.on("message", function(m) { var max = parseInt(m, 10), ops = max, temp = 0; while(ops--) { temp += Math.random(); if (ops % (max/10) === 0) { process.send(ops + " ops complete"); } } });
  • 25. Takeaways ● All JavaScript runtimes expose a single- threaded event loop ● Build your system around events and asynchronous callbacks ● Various runtimes provide additional tools for handling concurrency ● Just one approach - there are many more!

Editor's Notes

  • #9: The JavaScript runtime is single-threaded. Meaning: your code is only doing one thing at a time. You need to write your code