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

后端 未结 1 1702
情书的邮戳
情书的邮戳 2021-02-11 22:12

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 (dif

1条回答
  •  忘了有多久
    2021-02-11 22:27

    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.

    0 讨论(0)
提交回复
热议问题