requestAnimationFrame-vs-setImmediate Polyfills

Benchmark created by Jason Miller on


Description

Test out common requestAnimationFrame pollyfill VS setImmediate for the use-case of deferring execution of a function by the shortest amount of time possible.

Preparation HTML

<script>
  var rAF = window.requestAnimationFrame ||
            window.webkitRequestAnimationFrame ||
            window.mozRequestAnimationFrame ||
            function(fn){ setTimeout(fn, 10); };

  var setImmediate = window.setImmediate ||
    function(fn) {
      var img = new Image();
      img.onerror = fn;
      img.src = '';
    };

var async;
setImmediate(function() {
  async = true;
});
async = false;
setTimeout(function() {
  console.log(async);
  if (async===false) {
    throw new Error('setImmediate is not asynchronous.');
  }
}, 100);
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
requestAnimationFrame
// async test
rAF(function() {
  deferred.resolve();
});
ready
setImmediate
// async test
setImmediate(function() {
  deferred.resolve();
});
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 1: published by Jason Miller on
  • Revision 2: published by Jason Miller on