Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it accurate to say JavaScript is a "single-thread" language?

Tags:

javascript

I've heard this kind of statements many times, but personally I think this doesn't quite make sense. I think people are confusing JavaScript as a language specification and JavaScript in practice (browser, Node, etc.). Of course in most cases JavaScript are executed in a single-thread environment; but AFAIK nothing in the language specification requires it to be so. I think this is just like saying Python is "interpreted", while it's in fact entirely a matter of implementation.

So, is it accurate to say JavaScript is a "single-thread" language?

like image 334
Derek Chiang Avatar asked Jul 09 '13 07:07

Derek Chiang


3 Answers

By JavaScript you seem to mean ECMAScript.

There's already multithreading in the browser, built with webworkers, and based on a strong isolation of data : workers only communicate by message passing, nothing is shared.

If you want more intricate multithreading, with data sharing, then that doesn't look possible now. Nothing in ECMAScript explicitely forbids multithreading but you can't do multithreading without

  • facilities to create "threads" (in a general sense, that could be coroutines)
  • mutexes and facilities to synchronize accesses
  • a low level support to ensure for example a property change won't break the data in case of simultaneous accesses. None of the current engines has been designed with this kind of strength (yes, some of them support multiple threads but in isolation).

The fact ECMAScript wasn't designed to include multi-threading is enough to prevent, currently, to support it (other than message-passing isolated multi-threading as is already done but it's a very limited kind of multi-threading).

You have to realize that

  • data sharing multi-threading is very expensive (not even speaking about simultaneous actions on the DOM)
  • you would rarely use it in JavaScript

Why do I say you would rarely use it ? Because most of the IO blocking tasks (file reading, requests, db queries, etc.), most of the low level tasks (for example image decoding or page rendering), most of the UI management (with event queue), most of the scheduling (timeouts and intervals) is done outside for you.

like image 162
Denys Séguret Avatar answered Oct 23 '22 20:10

Denys Séguret


Multi-threading behavior is available in both HTML5 and node.js, BUT there is no native threading API in the Javascript language, so I guess the answer to your contrived question (I mean that in the nicest possible way, of course) is "yes, Javascript is a single-threaded language."

like image 45
Yusuf X Avatar answered Oct 23 '22 22:10

Yusuf X


AFAIK nothing in the language specification requires it to be so.

In TC39 it says:

At any point in time, there is at most one execution context per agent that is actually executing code.

That appears to me to be the crucial guarantee that you never have to synchronize access to variables in ECMAScript. Which is what I expect is meant when someone says a language is single-threaded.

Of course, most ECMAScript host environments use more than one thread in their host environment implementation for things like garbage collection, etc. And, ECMAScript itself allows for multiple separate contexts of execution that could each be driven by their own thread--though the standard makes it clear you could also just drive all of them with the same thread.

The point, again, is you never have to protect any ECMAScript variable with a mutex, semaphore, or the like (which is why ECMAScript provides no such facilities) since the language promises there will never be two threads of control with simultaneous access to the same context.

I don't know of any JavaScript implementation that violates this promise either, though there certainly could be.

like image 1
Ron Burk Avatar answered Oct 23 '22 22:10

Ron Burk