Rethinking Asynchronous JavaScript

Single Threaded JavaScript

Kyle Simpson

Kyle Simpson

You Don't Know JS
Rethinking Asynchronous JavaScript

Check out a free preview of the full Rethinking Asynchronous JavaScript course

The "Single Threaded JavaScript" Lesson is part of the full, Rethinking Asynchronous JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

In the context of programming, Parallelism is the utilization of multiple threads in an operating system. Routines are able to run at the same time regardless of execution order. JavaScript, however, is single threaded and only one line of code can be executed at any given time. Kyle explains these concepts to build a basis for understanding asynchronous programming.

Preview
Close

Transcript from the "Single Threaded JavaScript" Lesson

[00:00:00]
>> [MUSIC]

[00:00:03]
>> Kyle: All right, so let's jump in. Let's talk about parallel versus async. Metaphorically, if we try to understand what parallel is versus async, most people conflate those and think that they're basically the same thing or JavaScripts version of parallel is called async or whatever. It's not really the case.

[00:00:20]
Only at the highest level if you really squinted from 50,000 feet up. Might you look at parallelism and asynchronicity and say they're the same thing. There's important differences. So let me give you metaphorically how to kind of work through that. Parallelism versus non-parallelism, which we'll sort of group asynchronicity into non-parallelism for now and I'll explain that more.

[00:00:43]
So non-parallelism would be like you waiting in line at amusement park for a roller coaster. Lots of people in line and you finally get up to the front of the line. There's a roller coaster there with 30 seats on it but they only let you on. And you're the only person that can ride at any given point.

[00:01:01]
So even though there's capacity for 30 people to ride, at any given moment, only one person is riding the ride. That would be non-parallel. We'll give those different terms in a moment. They'd be non-parallel versus if you waited in line. You got up there and then 30 people loaded onto the roller coaster.

[00:01:17]
And when the roller coaster is running, all 30 of you are experiencing the roller coaster at exactly the same instant. That's more parallelism. So in a computing sense, parallelism is expressed through threads. Most commonly expressed through threads. The idea that I could have one thread on one CPU core in my system.

[00:01:35]
And another thread. They're are kind of like queues of actions that need to happen, or operations and need to happen. At any given instant, one core could be doing one of those operations, and in exactly that same moment, another operation could be happening on a different core. That's true.

[00:01:51]
Honest to goodness parallelism. Now, even with the most powerful of the machines that you may have out there. You may have a 16 core, or maybe you have a 32. Most of us probably have 4, 8 cores in our computers. 32 threads is not nearly enough to run a modern operating system with the types of thing, the multitasking that we do with running all the programs we run.

[00:02:12]
Even your browser, your single browser might take up 10 or 15 threads just for basic operations that it does on a web page. So if you've ever opened up the Task Manager of Chrome and seen all the processes that are running, it runs an awful lot of stuff In parallel.

[00:02:27]
So because we don't have infinite numbers of cores, the operating system actually has a layer that we would call sort of virtual threads. And it takes care of scheduling the virtual threads. It can hand out tens of thousands of those. And it takes care of trying to schedule those across the cores so that they're happening as and as much in parallel as possible.

[00:02:46]
From the perspective of your programming, you don't really care about that or even know what cores they're running on. You just assume that these two things are on separate threads, that the operating system will do the right thing and make sure that they're running separately. Right, so obviously parallelism is about optimization, right?

[00:03:02]
If I've got a bunch of things to do. I don't wanna wait to do the second set of tasks until the first set of tasks is finished, if it's possible to do both at the same time, if I can get done faster, that's much better. Parallelism is about optimization.

[00:03:16]
All right, so that explains parallelism. What about asynchronicity? Well, asynchronicity, the world that we live in, in asynchronicity, moves us to a single thread. The programming inside of our JavaScript runs entirely on a single thread even though the browser may have access to multiple threads and even the JavaScript engine node for example could use hundreds of threads in the background.

[00:03:37]
But our program only runs in a single thread. At any given instant there is only one line of JavaScript running in the JavaScript engine. You could in theory spend multiple instances of the JavaScript engine on entirely separate threads and yes that would be that would look like multithreaded programming but they couldn't communicate in any useful way.

[00:03:59]
Web workers is an example of trying to bridge that gap. Web workers is a way of spinning up an entirely separate thread but that's not a JavaScript thing, that's a browser thing. The web platform defines the notion of web workers. Spins up in a whole separate thread, a whole separate instance of the JavaScript engine.

[00:04:17]
As JavaScript is concerned, those two things don't know about each other they don't have any overlap, they're not sharing variables, there's nothing. The good news is, we don't have to worry about any of the nonsense that threaded programming as to where. We don't have to deal with new taxes and semaphores and all of the craziness and shared resource locking.

[00:04:34]
We know that only one function can be running in a given time. And if you have two separate JavaScript engines running, it doesn't matter because they're not gonna be operating over the same shared space anyway. Now, web workers fortunately do give us a communication path through asynchronous events.

[00:04:51]
So even though they are functionally operating on independent threads. They cannot communicate in a threaded fashion, they have to communicate back on that single threaded notion through the event loop.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now