Course contentsShow
← JavaScript
Lesson 2190 of 2,72345. Workers and ConcurrencyFree lesson

CPU-Bound vs I/O-Bound Tasks

Understand the difference between CPU-intensive and I/O-bound work, and why workers help with the former.

CPU-Bound vs I/O-Bound Tasks

What you'll learn: You'll understand the two fundamental types of computational work and why only one really benefits from worker threads.

Two Flavors of Work

When your program does something, it's usually either CPU-bound or I/O-bound:

  • CPU-bound tasks keep the processor busy computing. Examples: image processing, encrypting data, calculating millions of prime numbers, or running complex simulations. The CPU is churning through instructions.

  • I/O-bound tasks spend most of their time waiting for external operations to complete. Examples: fetching data from a database, reading files, making HTTP requests, or waiting for user input. The CPU is mostly idle while waiting for the disk, network, or other subsystem.

Why Workers Help CPU-Bound Tasks

JavaScript's event loop handles I/O-bound work efficiently through callbacks, promises, and async/await. While one async operation waits, the event loop can process other tasks. You don't need workers for I/O — the single-threaded model already handles waiting well.

Workers shine with CPU-bound tasks. If you need to process a 50MB image or calculate complex physics, that computation blocks the main thread. Users see frozen UIs. By offloading that heavy computation to a worker thread, the main thread stays responsive while the worker's CPU core grinds through the math.

The Analogy

Think of a restaurant: I/O-bound work is like waiting for the oven to bake a cake (one chef can start multiple cakes and check on them). CPU-bound work is like hand-kneading dough for an hour straight (you need another chef to do that while the first keeps serving customers).

Key Takeaway: Workers help with CPU-bound tasks that block the thread with computation, not I/O-bound tasks that already benefit from JavaScript's async event loop.