goog.timer.callImmediate
JavaScript performance comparison
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.
| Test | Ops/sec | |
|---|---|---|
callImmediate |
|
pending… |
setTimeout |
|
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:
- Revision 1: published by Malte
- Revision 2: published
- Revision 4: published
- Revision 5: published by Malte
0 comments