Skip to content

Instantly share code, notes, and snippets.

@wwalser
Created May 17, 2012 11:58
Show Gist options
  • Save wwalser/2718412 to your computer and use it in GitHub Desktop.
Save wwalser/2718412 to your computer and use it in GitHub Desktop.
Cross browser setImmediate speed tests
/**
* Did some performance tests using the absolutely awesome cross browser setImmediate
* implementation at https://github.com/NobleJS/setImmediate.
*
* I noticed that in Firefox performance of setImmediate remained steady and consistently
* outperformed the setTimeout(fun, 0) trick. Chrome was a different story with performance initially
* good but quickly diminishing. I found that if don't let Chrome use MessageChannel (the
* implementation it defaults to) it falls back to what Firefox uses and performance stabilizes.
*
* Below is test code you can run in developer tools and jsPerf test cases can be found at:
* (message channel, slow) http://jsperf.com/setimmediate-with-message-channel
* (no message channel, fast) http://jsperf.com/setimmediate-without-message-channel
*/
function createContinuation(){
var recurseMethod = Array.prototype.shift.call(arguments),
args = arguments,
count = 0,
continuation = function(){
count++;
recurseMethod.apply(window, args);
}
Array.prototype.unshift.call(args, continuation);
continuation();
return function(){
return count;
}
}
var timeoutCount = createContinuation(setTimeout, 0);
var immediateCount = createContinuation(setImmediate);
setInterval(function(){
console.log('timeout: ' + timeoutCount());
console.log('immediate: ' + immediateCount());
console.log('delta: ' + (immediateCount() - timeoutCount()));
console.log('percentage: ' + (timeoutCount()/immediateCount())*100);
}, 5000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment