Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single thread concept of JavaScript running in browser

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.

enter image description here

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

like image 421
Geek Avatar asked May 25 '13 12:05

Geek


People also ask

Is JavaScript single threaded in browser?

JavaScript is a single-threaded language because while running code on a single thread, it can be really easy to implement as we don't have to deal with the complicated scenarios that arise in the multi-threaded environment like deadlock. Since, JavaScript is a single-threaded language, it is synchronous in nature.

How JavaScript runs using a single thread?

Javascript is a single threaded language that can be non-blocking. Single threaded means it has only one call stack. Whatever is on the top of the call stack is run first. In the above program, functions are run sequentially.

Is browser single threaded?

Browsers aren't single threaded, but your script runs in a single runloop.

What is threading JavaScript?

JavaScript Thread is single-threaded by default used to run a script in the web application, perform layout and garbage collection. Being single-threaded is to execute only a single set of instructions at any time in the process.


1 Answers

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

like image 50
alexandernst Avatar answered Sep 17 '22 15:09

alexandernst