Transferrable vs Cloning

JavaScript performance comparison

Revision 2 of this test case created by

Preparation code

<script>
  var __finish;

  function createElements() {
    var total = 100000;
    var elements = new Uint32Array(total);

    for (var i = total; i > 0; i--){
      elements[i - 1] = Math.floor(Math.random() * 10000);
    }
    return elements;
  };

  function setupTransferrableWorker(){
    var wCode = function(event){
      var elements = event.data;
      postMessage(elements, [elements.buffer]);
    };

    var blob = new Blob(["onmessage = " + wCode.toString()]);
    var blobURL = window.URL.createObjectURL(blob);

    return new Worker(blobURL);
  };

  function setupCloningWorker(){
    var wCode = function(event){
      var elements = event.data;
      postMessage(elements);
    };

    var blob = new Blob(["onmessage = " + wCode.toString()]);
    var blobURL = window.URL.createObjectURL(blob);

    return new Worker(blobURL);
  };

  var elementsA = createElements();
  var elementsB = createElements();
  var a = true;
  var elements = elementsA;

  var cloningWW = setupCloningWorker();
  cloningWW.onmessage = function(event) {
    __finish();
  }

  var transferrableWW = setupTransferrableWorker();
  transferrableWW.onmessage = function(event) {
    if (a) { // https://esdiscuss.org/topic/arraybuffer-neutering
      elementsA = event.data;
      elements = elementsB;
    } else {
      elementsB = event.data;
      elements = elementsA;
    }
    a = !a;
    __finish();
  }
</script>
      
<script>
Benchmark.prototype.setup = function() {
  __finish = function () {
    deferred.resolve();
  }

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Cloning
// async test
cloningWW.postMessage(elements);
pending…
Trasnferrable
// async test
transferrableWW.postMessage(elements, [elements.buffer]);
pending…

Compare results of other browsers

Revisions

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

0 Comments