Workers demo is slow

Hi, i think you should consider modifying the workers demo as it is
slow in some browsers.

I filed a bug report some time ago:
http://code.google.com/p/chromium/issues/detail?id=85686

They came out that the problem is too much communication between the
worker and the main page. This ends up slowing main page too in the
end.

- An initial solution would be to report primes only one prime number each 1000.

- A better solution would imply taking time into account (eg: limit to
10 messages/second). This is my implementation of it:

function run() {
 var n = 1;
 var lastTime = 0;

 search: while (running) {
   n += 1;
   for (var i = 2; i <= Math.sqrt(n); i += 1)
     if (n % i == 0)
      continue search;

   // found a prime!
   var d = Date.now();
   if ( d - lastTime > 100 ) {
    lastTime = d;
    postMessage(n);
   }
 }
}

Received on Sunday, 24 June 2012 21:15:09 UTC