![]() |
When talking about JavaScript, sooner or later two often confused concepts come up:concurrency andparallelism.
Sometimes they are used as synonyms, but in reality they describetwo completely different ways of handling multiple tasks.
Understanding this difference is essential for writing more efficient code, avoiding unnecessary blocks and choosing the right architecture for complex applications.
π What is concurrency really?
Concurrency is the ability of a system to handle multiple tasks not at the same time, but alternating them quickly in order to maintain fluid execution.
Imagine a factory with a single machine that has to work on several parts. He can't work on two pieces at the same time, so he uses an alternating strategy:
-
He works a little on piece A
-
He puts it aside
-
He works a little on piece B
-
puts it aside
-
returns to piece A
-
and so on…
Production progresses on multiple pieces, even though there is only one machine.
The simultaneity is only apparent: progress occurs at “micro-shifts”.
JavaScript works the same way:
it has a single thread that alternates the execution of tasks via the event loop, intelligently handling asynchronous operations such as timers, HTTP requests, events, etc.
π What is parallelism?
Parallelism is the execution real and simultaneous of multiple tasks, made possible by hardware with multiple computing units (threads, cores, or CPUs).
This time, imagine a factory with two or more identical machines, each capable of working autonomously.
-
Machine 1 works on part A
-
Machine 2 works simultaneously on part B
The two machines operate at the same time, without alternating.
This actually increases total production because the tasks proceed in parallel.
In JavaScript, this only happens using:
-
Web Workers (in the browser)
-
Worker Threads (in Node.js)
In that case, we add "new machines" capable of working alongside the main thread.
π Key Differences
-
Concurrency → a single machine that alternates parts.
-
Parallelism → multiple machines working simultaneously.
-
Basic JavaScript → concurrency via event loops.
-
JavaScript with Workers → real parallelism via separate threads.
π» JavaScript Examples
JavaScriptis not parallel by nature, because its runtime (browser or Node.js) executes the main code on a single thread.
But…
JavaScriptcan perform parallelism by delegating to Worker Threads or Web Workers, which run on separate threads.
π Concurrency in JavaScript
Why is this concurrency?
The main thread executes Start, then delegates the timeout to the external environment (browser or Node).
The asynchronous task is queued and resumed later, when the thread is free.
No computation occurs in parallel: the thread alternates the execution of tasks just like the single machine in the factory switching from one part to another.
It's pure concurrency: intelligent interleaving, not concurrency.
⛓️ Real Parallelism with Web Workers (in the Browser)
Web Workers allow you to move heavy computations off the main thread, avoiding freezes and ensuring real parallelism.
π§ͺ Example: a Web Worker that eliminates a heavy loop from the main thread
main.js
worker.js
Why is this parallelism?
The worker runs the heavy loop in a separate thread, while the main thread continues to process logs, events, and UI.
It's like having two autonomous machines working on different parts at the same time.
The load is effectively divided, and the total time is reduced.
π―♀️ Parallelism in Node.js with Worker Threads
main.js
heavy.js
const { parentPort } = require("node:worker_threads");
parentPort.on("message", () => {
let count = 0;
for (let i = 0; i < 1e9; i++) count += i;
parentPort.postMessage(count);
});Why is this parallelism?
Unlike classic asynchrony, a real operating system thread is created here.
The main thread does not alternate tasks: it leaves part of the work to another "machine" that operates independently.
π JavaScript is concurrent, not parallel… until needed
Here's a simple and useful summary:
| Concept | How it works in regular JS | When to use it |
|---|---|---|
| Competition | Event loop, timers, async/await, fetch |
I/O, network, UI, animations |
| Parallelism | Web Workers / Worker Threads | Heavy calculations, CPU-bound, data analysis |
π Conclusions
-
Concurrency allows JavaScript to handle many operations by alternating them rapidly, ideal for I/O and asynchrony.
Parallelism is used to take advantage of multiple compute units simultaneously, essential for CPU-heavy tasks.
JavaScript is natively concurrent, but can become parallel thanks to Web Workers and Worker Threads.
In other words:
✔ Concurrency = management alternation
✔ Parallelism = true concurrency
Follow me #techelopment
Official site: www.techelopment.it
facebook: Techelopment
instagram: @techelopment
X: techelopment
Bluesky: @techelopment
telegram: @techelopment_channel
whatsapp: Techelopment
youtube: @techelopment
