goog.timer.callImmediate

JavaScript performance comparison

Revision 7 of this test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var goog = {
    Timer: {
      defaultTimerObject: window
    },
    global: window,
    bind: function(fn, context) {
      return fn.bind(context);
    }
  };
  
  goog.Timer.getSetImmediate_ = function() {
    // Introduced and currently only supported by IE10.
    if (typeof goog.global['setImmediate'] === 'function') {
      return goog.bind(goog.global['setImmediate'], goog.global);
    }
    // Create a private message channel and use it to postMessage empty messages
    // to ourselves.
    var Channel = goog.global['MessageChannel'];
    // If MessageChannel is not available and we are in a browser, 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' && typeof window !== 'undefined' &&
        window.postMessage && document.createEvent) {
      /** @constructor */
      Channel = function() {
        // Make an empty, invisible iframe.
        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) {
          // Validate origin and message to make sure that this message was
          // intended for us.
          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);
      };
    }
    // Implementation for IE6-8. Script elements fires an asynchronous
    // onreadystatechange event when inserted into the DOM.
    if (typeof document !== 'undefined' && 'onreadystatechange' in
        document.createElement('script')) {
      return function(cb) {
        var script = document.createElement('script');
        script.onreadystatechange = function() {
          // Clean up and run the callback.
          script.onreadystatechange = null;
          script.parentNode.removeChild(script);
          script = null;
          cb();
          cb = null;
        };
        document.documentElement.appendChild(script);
      };
    }
    // Fall back to setTimeout with 0. In browsers this creates a delay of 5ms
    // or more.
    return function(cb) {
      goog.global.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);
  };

};
</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
callImmediate
// async test
var arr = new Array(100);
goog.Timer.callImmediate(function() {
  for(var i=100; i--;)
     arr[i]=i;
  
  deferred.resolve();
});
pending…
setTimeout
// async test
var arr = new Array(100);
goog.Timer.callImmediate(function() {
  for(var i=100; i--;)
     arr[i]=i;
  
  deferred.resolve();
});
pending…
// async test
var arr = new Array(100);
goog.Timer.callImmediate(function() {
  for(var i=100; i--;)
     arr.push(i);
  
  deferred.resolve();
});
pending…
// async test
var arr = new Array(100);
goog.Timer.callImmediate(function() {
  for(var i=100; i--;)
     arr[i]=i;
  
  deferred.resolve();
});
pending…
Goog Timer + Predefined
// async test
var arr = new Array(100);
goog.Timer.callImmediate(function() {
  for(var i=100; i--;)
     arr[i]=i;
  
  deferred.resolve();
});
pending…
setTimeout + Predefined
// async test
var arr = [];
setTimeout(function() {
  for(var i=100; i--;)
     arr[i]=i;

  deferred.resolve();
}, 0);
pending…
Goog Timer + Grow Array
// async test
var arr = new Array();
goog.Timer.callImmediate(function() {
  for(var i=100; i--;)
     arr.push(i);
  
  deferred.resolve();
});
pending…
Goog Timer + Grow Array (unconstructed)
// async test
var arr = new Array;
goog.Timer.callImmediate(function() {
  for(var i=100; i--;)
     arr.push(i);
  
  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.

0 Comments