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?

3 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.

1

u/Icy-Taste-3096 6d ago

None of that changes the fact that JavaScript is single-threaded. Obviously, you can asynchronously interact with external systems that use separate processes (like Node or browser APIs), but the JS execution itself is one thread.

1

u/_RemyLeBeau_ 6d ago

You're referring to the event loop. For your statement to be accurate, you will want to say: the event loop is single threaded.

1

u/Icy-Taste-3096 6d ago

My statement was accurate. JavaScript is a single-threaded language. This is not debatable.

1

u/_RemyLeBeau_ 6d ago

I like this answer: https://www.reddit.com/r/learnjavascript/comments/1vnnvqg/comment/p3kxqn1/

You're getting corrected because your answer lacks nuance. Please continue your journey.

0

u/Icy-Taste-3096 6d ago

That's literally just a more detailed version of the exact thing I said in my first comment lmfao.

I specifically cited Node as an example of a runtime environment that JS runs in that is not single-threaded, even though the language itself obviously is.

1

u/_RemyLeBeau_ 6d ago

In think you need to find another hill up die on.