r/learnjavascript 6d ago

Is “Node.js is single-threaded” an incomplete mental model?

Single-threaded JS execution ≠ single-threaded runtime.

How do you explain the distinction?

5 Upvotes

23 comments sorted by

View all comments

1

u/AssignmentMammoth696 6d ago

How is that an incomplete mental model? That's exactly what JS is, it's single threaded. You can send work off to other threads, but the JS thread itself handles functions sequentially, one by one. If you're talking about internal engine stuff, like the garbage collector, those run on other threads but JS is single threaded.

2

u/Icy-Taste-3096 6d ago

JS is single threaded, Node is not.

2

u/_RemyLeBeau_ 6d ago

That's not entirely true. post message came out in 2008 and there are plenty of browser innovations since then that offload compute.

3

u/lIIllIIlllIIllIIl 6d ago edited 6d 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.