6

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?

3
  • keep in mind that even if the task is a "hello world", each webworker means to spawn another process and wait for another request to resolve. My notebook chokes when I have like 20 tabs open in chrome. I guess it wouldn't go much farther on webworkers Commented Aug 20, 2014 at 12:04
  • You can't spawn 500 web workers. Firefox, for example, has a limit of 20 web workers by default. Commented Aug 20, 2014 at 12:06
  • 1
    One worker per core, otherwise you won't gain anything. Of 500 workers there would at most be N active, where N is the number of cores. Commented Aug 20, 2014 at 12:08

1 Answer 1

8

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/

Sign up to request clarification or add additional context in comments.

1 Comment

Shouldn't the optimal number be less than navigator.hardwareConcurrency, as there will be many other processes running on the OS that will be competing for these threads?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.