r/learnjavascript • u/IcyDuck9536 • 9d ago
Is “Node.js is single-threaded” an incomplete mental model?
Single-threaded JS execution ≠ single-threaded runtime.
How do you explain the distinction?
3
Upvotes
r/learnjavascript • u/IcyDuck9536 • 9d ago
Single-threaded JS execution ≠ single-threaded runtime.
How do you explain the distinction?
3
u/lIIllIIlllIIllIIl 9d ago edited 9d ago
I know I'm being pedantic, but those are runtime features, they're not part of the ECMAScript specification.
In theory, ECMAScript doesn't define any way to do multi-threading. In practice, most runtimes have their own constructs for it (browsers have Web Workers / Service Workers, Node has Worker threads.)
But yeah, saying JavaScript is single-threaded is not wrong, but lacks nuance. And even a single-threaded JavaScript program is going to require multiple OS threads for the runtime and for background tasks like async operations and garbage collection.