Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do all tabs in a browser window share a single JavaScript thread?

In general, JavaScript execution in browser is considered as single-threaded. Does this single thread apply to all tabs open in a browser window?

In other words, if (different) JavaScript code are running in different tabs, they are all being executed using a single thread?

In addition, what happens when you have multiple windows of the same browser open and each window contains multiple tabs?

Finally, does the answers to the above depend on browser vendor/version etc?

like image 736
MLister Avatar asked Jun 18 '13 13:06

MLister


People also ask

Is a browser tab single threaded?

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

Is each Chrome tab a thread?

Chrome has a multi-process architecture and each process is heavily multi-threaded. The main goal is to keep the main thread (“UI” thread in the browser process) and IO thread (each process' thread for handling IPC) responsive.

Is Chrome JavaScript single threaded?

Well, Chrome is multiprocess, and I think every process deals with its own Javascript code, but as far as the code knows, it is "single-threaded". There is no support whatsoever in Javascript for multi-threading, at least not explicitly, so it does not make a difference.

Is Chrome multi-threaded?

Chrome has a multi-process architecture and each process is heavily multi-threaded. In this document we will go over the basic threading system shared by each process. The main goal is to keep the main thread (a.k.a. “UI” thread in the browser process) and IO thread (each process's thread for receiving IPC) responsive.


1 Answers

There is no way to answer that in a generic way because this is browser implementation specific.

Pretty much every older browser always used a single thread for every tabs, but more modern browsers / versions might have changed that (for example, chrome has a thread per tab - actually, it even has a whole process per tab). EDIT: correction from the comment

Actually chrome uses Process-per-site-instance. That means a single site opened in multiple tabs will still get rendered by the same process

If you are asking it for performance reasons (kind of like asking "it is ok to block everything in my website using an eternal infinite loop, or will that spread to other tabs"), it is safer to assume that the thread is shared by everyone. If it is in the current browser then you planned for it, and if it isn't then you get better performance than planned for, hardly a problem.

In order to get some code running in its own thread, have a loop at Web Workers, but they are still far from being fully implemented in every "modern" browsers.

like image 64
Lepidosteus Avatar answered Oct 01 '22 14:10

Lepidosteus