Search code examples
javascriptweb-worker

Web workers in Javascript, number of threads and workers?


If I understand it right Web Workers run in a single isolated thread, therefore I don't get the point of creating multiple Workers.

I have found this demo https://nerget.com/rayjs-mt/rayjs.html , that shows performance benefits of using workers for rendering a cube on a canvas.

I have tried to use 5 and 15 Workers. Don't see any significant difference in rendering speed.

Is there any point to create a lot of Web Workers? If yes, what would be the right number?

Any help will be appreciated!


Solution

  • A worker may run on another thread, or may not. If you create too much threads, they don't actually run at the same time: this behaviour is emulated. Because of the time required to send / receive messages to / from a worker, it might actually hurt your performances to create too much workers.

    More informations can be found here.

    It is important to distinguish software threads from hardware threads. Software threads are the threads that programs create. Hardware threads are real physical resources. There may be one hardware thread per core on the chip, or more, as for example with Intel Hyper-Threading Technology.

    When there are more software threads than hardware threads, the operating system typically resorts to round robin scheduling. Each software thread gets a short turn, called a time slice, to run on a hardware thread. When the time slice runs out, the scheduler suspends the thread and allows the next thread waiting its turn to run on the hardware thread.