Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Web Workers utilize 100% of a multi-core CPU?

I've been trying to find out just how capable web workers are of distributing processor load. I've yet to find any demos that seem to be able to get my quad core 2600k to even 50%, let alone 100%.

Here's a web worker demo I've tried to max my CPU on:

http://nerget.com/rayjs-mt/rayjs.html

(If you go into the page's HTML with firebug /chrome-inspect-element and make the canvas larger, you can make it raytrace a much larger image - I set mine to 1920 x 1080)

Even with 4, 8, 16 workers selected, I can't get my CPU utilization above around 25% per core.

Does anyone know if you can utilize 100% of the CPU through web workers?

(I'm using Google Chrome.)

like image 376
BumbleShrimp Avatar asked Aug 08 '12 19:08

BumbleShrimp


People also ask

Are web workers multithreaded?

Web workers give us the ability to write multi-threaded Javascript that doesn't block the DOM. To some extent, even asynchronous operations block the DOM.

Does Web browsing use multiple cores?

Do web browsers benefit from more cores? Yes. Modern web browsers spawn a new thread (or sometimes several) per tab that is opened. Browsers are very multi-threaded when we use them in a parallel manner (lots of active tabs/windows open).

How many CPU cores do I need for work?

If you're running into bottlenecks in common desktop use, it's less likely to be because you don't have enough CPU cores and more likely due to something like not having enough RAM, especially if you're doing very heavy web browsing. So for common desktop use, 2-4 cores should be enough for your needs.

How many cores does a webserver need?

Server CPUs usually have upwards of 32 CPU cores, working together at the same time. Most websites don't need anywhere near 32 CPU cores. So, in most forms of hosting where server resources are shared, you'll see that hosting companies offer 'cores.


2 Answers

This uses 100% on my 2500K:

var code = "while(true){}";
var URL = window.webkitURL || window.URL;
var bb = new Blob([code], {type : 'text/javascript'});

code = URL.createObjectURL(bb);

new Worker(code);
new Worker(code);
new Worker(code);
new Worker(code);

http://jsfiddle.net/MTJ27/81/

like image 118
Esailija Avatar answered Oct 18 '22 06:10

Esailija


I have re-written Esailija's answer using the new blob constructor. BlobBuilder is now outdated, so you must use Blob() instead, see here for the deets: http://updates.html5rocks.com/2012/06/Don-t-Build-Blobs-Construct-Them

window.URL = window.URL || window.webkitURL;

var blob = new Blob(["while(true){}"], {type: 'text/javascript'});

code = window.URL.createObjectURL(blob);

new Worker(code);
new Worker(code);
new Worker(code);
new Worker(code);

http://jsfiddle.net/MTJ27/15/

like image 42
StuR Avatar answered Oct 18 '22 06:10

StuR