Vadim 🦄 A.’s Post

"JavaScript is single-threaded" We don't call C++ or Go "single-threaded" — yet they execute in one thread by default too. Creating parallel threads requires explicit code in any language. So why do we label JavaScript this way? -- HISTORICAL CONTEXT -- In the 2000s, browsers themselves were single-process. One frozen tab = entire browser frozen. Chrome (2008) pioneered multi-process architecture. Firefox followed with Electrolysis only in 2016. The "JS is single-threaded" myth was born in that era — and stuck. -- MODERN JAVASCRIPT -- Today, JavaScript has full parallelism capabilities. Let's look at the tools. -- WEB WORKERS -- Separate threads with their own event loop. True parallel execution. const worker = new Worker('heavy-task.js'); worker.postMessage(data); worker.onmessage = (e) => console.log(e.data); Limitation: no DOM access, communication via postMessage (structured clone). -- SHARED MEMORY -- SharedArrayBuffer + Atomics enable lock-free concurrent programming: // Main thread const sab = new SharedArrayBuffer(1024); const view = new Int32Array(sab); worker.postMessage(sab); // Worker Atomics.add(view, 0, 1); // atomic increment Atomics.wait(view, 0, expectedValue); // block until changed Same memory model as C++/Rust. Race conditions included. -- WORKLETS -- Lightweight threads for specific tasks: • AudioWorklet — real-time audio processing • PaintWorklet — custom CSS painting • AnimationWorklet — off-main-thread animations -- NODE.JS -- Node.js is multithreaded out of the box. libuv thread pool: 4 threads by default (UV_THREADPOOL_SIZE, max 1024). Handles: file system, DNS, crypto. For custom parallelism: worker_threads. For multiprocessing: child_process, cluster. -- THE REAL CONSTRAINT -- DOM access is single-threaded. Not JavaScript itself. The event loop is scheduling, not a threading limitation. V8 runs on multiple threads — it just gives you one main thread by default. Same story as C++ or Go: one thread by default, explicit code for parallelism. #JavaScript #WebWorkers #NodeJS #Concurrency #Performance

  • diagram

Totally, in addition Service Workers and Shared Workers (limited availability) are special kinds of worker threads that allow phenomenal control of process and flow. Workers use the message API which incorporates the structuredClone method keeping objects immutable across worker threads. It's not quite as powerful as Kotlin coroutines, but JavaScript is vastly more powerful than most people believe.

Old and new knowledge/versions in conflict about not knowing about the way muti-threadedness is handled in browsers/JS/etc. I wouldn't call the web stack "good" in any way though. It's not weird people get lost in it.

Filip Vrba

Chaos & Tooling Engineer

2mo

Great deep dive! From a different angle, I usually just read “JS is single-threaded” as a shorthand for “event loop first, everything else is opt-in.” But love the detailed walkthrough — it’s always good to see why it works the way it does.

See more comments

To view or add a comment, sign in

Explore content categories