Crunch Web Worker Example

Dedicated Web Workers provide a simple means for web content to run scripts in background threads. Web workers are able to utilize multi-core CPUs more effectively.

Crunch can be loaded as a web worker, simply by providing the crunch script new Worker("crunch.js"). Messages are then used to request calculations and return results.

Using a web worker allows for several calculations to be done in parallel while not affecting page responsiveness.

Find Large Prime via Web Worker

Crunch is being used in web worker mode. To request a calculation a message is sent to the worker: worker.postMessage({"func": "nextPrime", "args": [Random N bit number]}).

Once the calculation is complete, a message containing the result is returned. This is demonstrated below.

The prime numbers generated on this page can be verified using the openssl library, for example: openssl prime NUMBER

« Back to Examples