3

I wrote an algorithm with JavaScript. When I execute it in Google Chrome, it takes about 30min, which is quite long.
When I open the task manager during the computation, I can see that Chrome takes about 25% of my CPU, so 1 of my 4 cores.

Is it possible, to allow Chrome to use more of my CPUs, say 3 of 4, so that the computation will be executed faster?

PS: I don't mind using any other browser (e.g. Firefox) if Chrome won't support that.

1 Answer 1

5

Standard JavaScript is uni-threaded: it can execute only one program on one CPU (core).

For what you are describing, you would probably have to partition your dataset into chunks and create a separate JavaScript Web Worker running your algorithm to process each chunk. This assumes that you have a structurally uniform dataset. (Watch out for boundary conditions.)

Using Web Workers effectively requires understanding how to rewrite serial algorithms as parallel and other aspects of multi-threaded programming--all of which require advanced programming skills.

JavaScript Web Workers are self-contained independent execution units (threads), separate and isolated from the main program and from one-another. Communication between the main program and Web Workers, and between Web Workers themselves, is through event-driven data messages.

The host operating system can execute individual Web Worker threads on separate cores, enabling up to 100% utilization of CPU capacity.

Web Workers are designed to be only units of computation. They do not have access to the DOM or any DOM-related browser components, such as window. (This avoids all kinds of potential problems with serializing access to them.) Thus, any output to be displayed must be passed back from a Worker to the main thread.

All recent browsers offer some degree of support for Web Workers. The table at the bottom of Mozilla's Web Worker API page, https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API, shows individual browser and version support for the various Web Worker features.

A good primer on using Web Workers:

http://www.html5rocks.com/en/tutorials/workers/basics/

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .