The following figure is taken from Chapter 3 of the book Secrets of the JavaScript Ninja by Jon Resig. Here the author is explaining the browser event loop.
The book has to say this :
It’s important to note that the browser mechanism that puts the events onto the queue is external to this event loop model. The processing necessary to determine when events have occurred and to push them onto the event queue doesn’t participate in the thread that’s handling the events.
So my question is it correct to say that JavaScript in browser is single threaded? I ask this question because clearly two separate tasks(processing the events and event queing are going on in parallel here).
The following figure is taken from Chapter 3 of the book Secrets of the JavaScript Ninja by Jon Resig. Here the author is explaining the browser event loop.
The book has to say this :
It’s important to note that the browser mechanism that puts the events onto the queue is external to this event loop model. The processing necessary to determine when events have occurred and to push them onto the event queue doesn’t participate in the thread that’s handling the events.
So my question is it correct to say that JavaScript in browser is single threaded? I ask this question because clearly two separate tasks(processing the events and event queing are going on in parallel here).
Share Improve this question edited Aug 16, 2020 at 15:17 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked May 25, 2013 at 12:41 GeekGeek 27.2k45 gold badges156 silver badges232 bronze badges 2-
Making above question (maybe) even more interesting might be the implications by so called immediate callbacks (as suggested in this answer). In short such a immediate callback would be Javascript code run, while other code halted by a blocking statement (i.e.
alert()
) has not yet run to pletion. By this it seems that the second task "event queuing" can introduce multi-thread problems (i.e. determinism). Good question! – humanityANDpeace Commented Jan 20, 2014 at 10:19 - 1 possible duplicate of Is javascript guaranteed to be single-threaded? – Liam Commented Jun 16, 2015 at 15:34
2 Answers
Reset to default 13JavaScript is single-threaded anywhere, in a browser or in NodeJS. It never was supposed to support multithreading in any way (and probably if somebody implements a JS engine with some kind of multithreading, bad things will happen, for sure)
EDIT to answer your edit:
That event queue is filled with data (mouse/kb events, network events, etc) from the main loop of the browser. That same main loop that runs JS. The figure you post is correct but it (kind of) blurs the reality. AFAIK Only one thread handles everything (that is, filling the queue and running, line-by-line, any JS code).
EDIT: One way to prove this: Create a really long loop and a text area. Try to write in the text are while the loop is running. You can't: it's because the main loop is busy running the loop so it can't handle the kb events.
EDIT: This seems to be a really good answer: Is JavaScript guaranteed to be single-threaded?
+2 years after the last EDIT: This answer is getting a little bit old and detached from reality. io.js (and node.js after that, probably Chrom[e|ium], FF, Safari after that) is pushing towards multiprocess support (via workers). You can check more about that here.
@alexandernst
One way to prove this: Create a really long loop and a text area. Try to write in the text are while the loop is running. You can't: it's because the main loop is busy running the loop so it can't handle the kb events.
This is happening because the event loop doesn't get to handle the events. If you wait for the loop to plete, you'll find all the text, you wrote while the the loop was running, appear.
That means that you have a separate thread picking up the input events and putting them on the queue.