(techno music) - 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 Java Script's version of Parallel is called Async or whatever. It's not really the case. 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. So, non-parallelism would be like you waiting in line at a 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 rollercoaster there with 30 seats on it, but they only let you on. And you're the only person that can ride at any given moment. 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. Will give those different terms in a moment, but it would be non-parallel. Versus if you waited in line, you got up there and 30 people loaded on to the rollercoaster and when the rollercoaster's running all 30 of you are experiencing the rollercoaster 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 and another thread, they are kind of like cues of actions that need to be happen or operations that need to happen.
At any given instant, one core could be doing one of those operations and at exactly that same moment, another operation could be happening on a different core. That's true, 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, maybe you have a 32 core, most of us probably have four or eight cores in our computers. 32 threads is not nearly enough to run a modern operating system with the types of things, the multi-tasking that we do with running all the programs we run.
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 run, and you know it runs an awful lot of stuff in parallel. 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 core, so that they're happening as much in parallel as possible.
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 these two things are on separate threads, that the operating system will do the right thing and make sure that they're running separately. All right, so obviously parallelism is about optimization. If I've got a bunch of things to do, I don't want to 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.
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 thread in notion, through the event loop.
Note: This course was created by Frontend Masters. It was originally released on 3/29/2016. We're pleased to host this training in our library.
- Parallel and asynchronous code
- Working with callbacks
- Using thunks
- Exploring promise flow control
- Abstractions, sequences, and gates
- Observables, events, and sequences
- Blocking channels