WebWorker vs Single Thread

Benchmark created by Cong Liu on


Test runner

Ready to run.

Testing in
TestOps/sec
Single thread
var sum = 0;
for (var i = 1; i <= 4000000; i++)
sum += i;
ready
Four thread
// async test
var threads = [];
var sum = 0;
var count = 4;
for (var i = 0; i < 4; i++) {
  var code = 'var sum = 0; for(var i = ' + (i * 1E6 + 1) + '; i<= ' + ((i + 1) * 1E6) + '; i++) sum += i;postMessage(sum);';
  var bb = new Blob([code], {
    type: 'text/javascript'
  });
  var bbURL = webkitURL.createObjectURL(bb);
  var worker = new Worker(bbURL);
  worker.onmessage = function(e) {
    sum += e.data;
    if (--count === 0) {
      deferred.resolve();
    }
  };
  threads.push(worker);
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.