Why are modern browser JS engines multi-threaded?

977 views Asked by At

I understand modern browsers' JS engnines (like V8, Spidermonkey, Chakra, etc.) use thread pools internally, even though only a single thread (running the event loop) is exposed to a JS programmer.

Obviously, the (rarely used) Web Workers require multiple threads (or multiple processes) - otherwise they couldn't utilize multiple CPU cores. My question is, apart from Web Workers, what is the benefit of implementing JS engine with multiple threads?

Why couldn't JS engine remain always single-threaded by internally relying on the same event-loop that the JS programmers use, using non-blocking OS calls whenever it needs to do any IO?

To clarify: JS engine uses a thread pool even if the user opened just one window with just one tab.

Edit: this is answered here

1

There are 1 answers

2
Bergi On BEST ANSWER

There are many parts of a script engine that benefit from parallelisation, as they can run concurrently for different parts of the script or in relation to each other:

  • parsing
  • compilation
  • JIT, optimisation
  • debugging/logging/profiling
  • garbage collection
  • graphics

And that doesn't even involve sharing between multiple instances of the engine for different usage environments (worker scripts, browsing contexts).