postMessage

JavaScript performance comparison

Revision 9 of this test case created

Preparation code

<script>
  var echoSetTimeout = (function() {
    return function(msg, cb) {
      setTimeout(cb, 0);
    }
  })();

 var echoAnimFrame = (function() {
    var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
    return function(msg, cb) {
      requestAnimationFrame(cb);
    }
 })();
 
  var echoWorker = (function() {
    var code = 'onmessage = function(e) { postMessage(e.data) };';
    var blobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder;
    if(!blobBuilder) {
      return echoSetTimeout;
    }
    var bb = new (blobBuilder)();
    bb.append(code);
 
    var blobURL = (window.URL || window.webkitURL || window.mozURL).createObjectURL(bb.getBlob());
    var worker = new Worker(blobURL);
   
    return function(msg, cb) {
      worker.onmessage = cb;
      worker.postMessage(msg);
    };
  })();
 
  var echoIFramePostMessage = (function() {
    var iframe = document.createElement('iframe');
    window.onload = function() {
      iframe.style.display = 'none';
      document.body.appendChild(iframe);
      iframe.contentDocument.write('<html><head><script>onmessage = function(e) { e.source.postMessage(e.data, \'*\') };<'+'/script><'+'/head><'+'/html>');
    };
   
    return function(msg, cb) {
      window.onmessage = cb;
      iframe.contentWindow.postMessage(msg, '*');
    };
  })();
 
  var echoPostMessage = (function() {
    return function(msg, cb) {
      async = false;
      window.onmessage = cb;
      window.postMessage(msg, '*');
      async = true;
    };
  })();

  var echo
 
 
  var payload = {
    shortString: 'test',
    simple: {
      foo: "bar",
      arr: []
    }
  };
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Worker shortString
// async test
echoWorker(payload.shortString, function resolve() {
  deferred.resolve();
});
pending…
iframe shortString
// async test
echoIFramePostMessage(payload.shortString, function resolve() {
  deferred.resolve();
});
pending…
setTimeout shortString
// async test
echoSetTimeout(payload.shortString, function resolve() {
  deferred.resolve();
});
pending…
sameWindowPostmessage shortString
// async test
echoPostMessage(payload.shortString, function resolve() {
  deferred.resolve();
});
pending…
Animation Frame
echoAnimFrame(payload.shortString, function resolve() {
  deferred.resolve();
});
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. Here’s a list of current revisions for this page:

0 comments

Add a comment