goog.timer.callImmediate

JavaScript performance comparison

Revision 2 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var goog = {
      Timer: {
        defaultTimerObject: window
      },
      bind: function(fn, context) {
        return fn.bind(context);
      }
    };
   
    goog.Timer.getSetImmediate_ = function() {
      // Introduced and currently only supported by IE10.
      if (typeof goog.Timer.defaultTimerObject['setImmediate'] === 'function') {
        return goog.bind(goog.Timer.defaultTimerObject['setImmediate'],
            goog.Timer.defaultTimerObject);
      }
      // Create a private message channel and use it to postMessage empty messages
      // to ourselves.
      var Channel = goog.Timer.defaultTimerObject['MessageChannel'];
      // If MessageChannel is not available, implement an iframe based polyfill
      // in browsers that have postMessage and document.createEvent. The latter
      // excludes IE8 because it has a synchronous postMessage implementation.
      if (typeof Channel === 'undefined' && window.postMessage &&
          document.createEvent) {
        Channel = function() {
          var iframe = document.createElement('iframe');
          iframe.style.display = 'none';
          iframe.src = '';
          document.body.appendChild(iframe);
          var win = iframe.contentWindow
          var doc = win.document;
          doc.open();
          doc.write('');
          doc.close();
          var message = 'callImmediate' + Math.random();
          var origin = win.location.protocol + '//' + win.location.host;
          iframe.contentWindow.onmessage = goog.bind(function(e) {
            if (e.origin != origin && e.data != message) {
              return;
            }
            this['port1'].onmessage()
          }, this);
          this['port1'] = {};
          this['port2'] = {
            postMessage: function() {
              win.postMessage(message, origin);
            }
          };
        };
      }
      if (typeof Channel !== 'undefined') {
        var channel = new Channel();
        // Use a fifo linked list to call callbacks in the right order.
        var head = {};
        var tail = head;
        channel['port1'].onmessage = function() {
          head = head.next;
          var cb = head.cb;
          head.cb = null;
          cb();
        };
        return function(cb) {
          tail.next = {
            cb: cb
          };
          tail = tail.next;
          channel['port2'].postMessage(0);
        };
      }
      // Fall back to setTimeout.
      return function(cb) {
        goog.Timer.defaultTimerObject.setTimeout(cb, 0);
      }
    };
   
    goog.Timer.callImmediate = function(listener, opt_handler) {
      var callback = listener;
      if (opt_handler) {
        callback = goog.bind(listener, opt_handler);
      }
      if (!goog.Timer.setImmediate_) {
        goog.Timer.setImmediate_ = goog.Timer.getSetImmediate_();
      }
      goog.Timer.setImmediate_(callback);
    };
   
    var dummy = document.createElement('div');
    var callbacks = [];
    var observer = new MutationObserver(function(e) {
      var cbs = callbacks;
      callbacks = [];
      for (var i = 0; i < cbs.length; ++i) {
        cbs[i]();
      }
    });
    observer.observe(dummy, {attributes: true});
   
    function scheduleObserverCallback(fn) {
      cbs.push(fn);
      dummy.hidden = !dummy.hidden;
    }
};
</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
callImmediate
// async test
goog.Timer.callImmediate(function() {
  deferred.resolve();
});
pending…
setTimeout
// async test
setTimeout(function() {
  deferred.resolve();
}, 0)
pending…
MutationObserver
// async test
scheduleObserverCallback(function() {
  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