30

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?

MLister
  • 10,022
  • 18
  • 64
  • 92
  • 1
    I think your last question answers it all. – HamZa Jun 18 '13 at 13:11
  • 1
    Also I think your question is best suited for [Security SE](http://security.stackexchange.com). – HamZa Jun 18 '13 at 13:13
  • 1
    @HamZa, besides security concern, it is related to performance/behavior of your JS code, as if it is single-threaded across tabs, then some of your code (e.g. `setTimeout`) may be delayed which could produce undesirable behavior. – MLister Jun 18 '13 at 13:16
  • You answered your own question at the end and I confirmed it. It's all browser/version specific. Checking each browser and their change log might help in understanding the new behaviors in each version. This can go forever since developers/security specialists develop new techniques to counter vulnerabilities/exploits. – HamZa Jun 18 '13 at 13:26
  • If it comes down to setTimeout being an issue for you, keep in mind that a less entangling option might be to compare time stamps instead. You could easily write your own robust function to do so. – eatonphil Jun 18 '13 at 13:35
  • 1
    Although not strictly a programming question, this topic is very relevant to all of us who program with JavaScript. I would encourage grass roots support to reopen this question. – Marc Audet Aug 01 '13 at 15:39

2 Answers2

13

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.

Lepidosteus
  • 11,779
  • 4
  • 39
  • 51
  • thanks for the answer. So Chrome (all versions?) is clear, what about `FF 11+` and `IE 9+`, `Safari 5+`? They are all generally considered as 'modern' browsers. – MLister Jun 18 '13 at 13:20
  • 7
    Actually chrome uses Process-per-site-instance. That means a single site opened in multiple tabs will still get rendered by the same process. – Patryk Ziemkowski Jun 18 '13 at 13:22
  • @MLister: it is really difficule to answer the question properly. For exemple, IE 8 (I don't know if that changed with further versions) started several processes (2 at browser start, and more as needed), but the actual count of processes did not match the number of tabs. So you could have 2 tabs sharing a process, 4 sharing another ... – Lepidosteus Jun 18 '13 at 13:23
  • 1
    IE 8 Process Model: http://blogs.msdn.com/b/ie/archive/2008/03/11/ie8-and-loosely-coupled-ie-lcie.aspx – Thomas Junk Jun 18 '13 at 13:24
  • @PatrykZiemkowski: thanks, added an edit to my answer. Things like that really highlight the "it depends". – Lepidosteus Jun 18 '13 at 13:25
  • https://www.chromium.org/developers/design-documents/process-models - the "site instance" is not the same term as origin, it is one instance per domain, not per origin – inf3rno Sep 09 '18 at 10:39
-1

If you look at the Javascript postMessage() e.g. window.postMessage('hi-there', location.origin), to create a message you have to add the origin. When you get a message event, you should check your origin with the origin that comes in the message because you might be getting messages from another tab. This would indicate that the browser itself is the main window. I'm not sure but it could be that when you change to a different tab, the prior Dom is saved and the new tab's Dom is activated.

Lin Du
  • 88,126
  • 95
  • 281
  • 483
Fritz
  • 343
  • 1
  • 10