Multiple StackOverflow answers say that the optimal number of web workers to use is equal to the number of physical cores in the machine, not the number of logical cores. This matches my personal testing; my Macbook has hyperthreading enabled and reports 4 physical cores and 8 logical cores, and I get maximum performance with 4 web workers, not 8.
Why is this? If hyperthreading is supposed to allow a single CPU core to function as though it were two cores by executing two instructions in parallel on each clock cycle, then why does it seem to have no effect on additional web worker threads?
Relatedly, the navigator.hardwareConcurrency
property in Javascript usually returns the number of logical cores, not physical cores. But its MDN page says that it exists specifically to provide a count of the optimal number of web workers:
The number of logical processor cores can be used to measure the number of threads which can effectively be run at once without them having to context switch. The browser may, however, choose to report a lower number of logical cores in order to represent more accurately the number of Workers that can run at once.
But as described in the first paragraph, it seems to be well-accepted among JS developers that the optimal number of web workers is more closely related to the number of physical cores than logical cores, and that the number reported by navigator.hardwareConcurrency
is misleading. There are even entire utilities devoted to figuring out the number of physical cores for this purpose.
What's going on here? Why does hyperthreading seem to not function for its intended purpose when it comes to running web workers? And given that this fact about hyperthreading is well-known, why does navigator.hardwareConcurrency
seem to ignore this fact and report an incorrect number?