Native Promises vs Callbacks

JavaScript performance comparison

Revision 22 of this test case created by Kyriakos-Ioannis Kyriakou

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var nextTick = (function () {
      var el = document.createElement('div');
      var cb;
      new MutationObserver(function () {
          cb();
      }).observe(el, { attributes: true });
      return function (callback) {
          cb = callback;
          el.setAttribute('x', 'y');
      };
  }());
  
  var cb = function (deferred) {
    nextTick(function () {
      deferred.resolve('data');
    });
  };
  
  var promise = function (deferred) {
    new Promise(function(resolve) {
      resolve('data');
    }).then(function (val) {
      nextTick(function () {
        deferred.resolve(val);
      });
    });
  };

};
</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
Callbacks
// async test
cb(deferred);
pending…
Promises
// async test
promise(deferred);
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