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

When Not to Use Workers

Recognize tasks too small or too I/O-heavy for workers, where communication overhead outweighs parallelism gains.

When Not to Use Workers

You'll learn: How to identify scenarios where using workers actually hurts performance instead of helping.

The Wrong Tool for the Job

Not every task benefits from parallelism. Workers introduce communication overhead—serializing data, transferring it between contexts, and deserializing on the other side. For small, quick tasks, this overhead can dwarf the actual work time, making your code slower than a simple synchronous approach.

Tasks That Don't Belong in Workers

Tiny Computations

If a calculation finishes in microseconds (like parsing a single JSON object or summing 100 numbers), shipping it to a worker wastes time. The postMessage round-trip takes longer than the work itself.

Analogy: Don't hire a delivery service to carry a pencil across the room. Walking there yourself is faster.

I/O-Heavy Operations

Workers don't make network requests faster or database queries return sooner. If your task spends 95% of its time waiting for I/O (fetching data, reading files), parallelism offers nothing. Use async/await on the main thread instead.

Frequent Communication

If your worker needs to send/receive messages hundreds of times per second (like real-time game physics that updates UI constantly), the messaging bottleneck negates any CPU gains.

Accessing DOM or Main-Thread APIs

Workers can't touch the DOM or use many browser APIs. If your task needs these, it must stay on the main thread. Don't force awkward back-and-forth communication patterns.

The Decision Test

Ask: "Does this task block the UI for 50ms+ and involve heavy computation and require minimal data transfer?" If any answer is "no," keep it simple.

Key Takeaway: Workers shine for heavy, CPU-bound tasks with infrequent communication. For small jobs, I/O-bound work, or DOM-dependent code, the overhead of workers makes them counterproductive—stick to the main thread.