Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optimal number of Web Workers

Suppose you have a task on your webapp that demands maximum performance. The task is highly parallelizable: you can subdivide it on, say, 500 independent subtasks. What is the best solution?

  • (A) Merely spawn 500 Web Workers.
  • (B) Spawn a small amount t of Web Workers and make them consume those 500 tasks.

If (B) is the way to go, how can you determine (empirically if needed) the best value for t? Is there any library to solve this particular problem?

like image 223
stag Avatar asked Aug 20 '14 12:08

stag


People also ask

How many web workers can I use?

You can spawn as many workers as you wish. You can also pass data to the script being executed in the worker threads and also return value to the main thread upon completion.

What are web workers good for?

Web Workers are primarily used for CPU-intensive tasks to be run in the background without any network connectivity required to work on the tasks.

Are web workers multithreaded?

Web Workers run in an isolated thread. As a result, the code that they execute needs to be contained in a separate file.

What is true about web workers?

Web Workers are a simple means for web content to run scripts in background threads. The worker thread can perform tasks without interfering with the user interface.


1 Answers

Use navigator.hardwareConcurrency for optimal number of web workers.

Browser support - http://caniuse.com/#search=hardwareConcurrency

Polyfill for non-supported browser versions - https://oswg.oftn.org/projects/core-estimator/demo/

like image 195
softvar Avatar answered Sep 28 '22 10:09

softvar