I'm looking at writing a long running CPU intensive operation using Web Workers and it does seem to be possible to push a client's CPU to 100% with these. (See Can Web Workers utilize 100% of a multi-core CPU?)
Does anyone know of any effective ways to limit the CPU utilization of web workers?
Indeed there is a way.
Structure your computations so they can be done by repeatedly calling a function which performs a part of the work before exiting. Just before exiting, use setTimeout to schedule a new call on the worker function in a few milliseconds. The wait time can be adjusted to use nore or less CPU time.
function doWork () {
var timer = new Date ();
// do n cyles of work here
timer = new Date () - timer; // time spent working
setTimeout (doWork, timer); // wait an equivalent time for 50% processor load
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With