promise comparisons (v144)

Revision 144 of this benchmark created by Billy Tetrud on


Description

This is a comparison of different promise libraries, performing the most basic tasks of creating a promise, adding a then handler and then resolving the promise.

Preparation HTML

<script>
  // pimp installs a global setImmediate shim
  var _setImmediate = window.setImmediate;
  var _Promise = window.Promise;
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="https://cdn.rawgit.com/dfilatov/vow/0.4.7/vow.min.js"></script>
<script src="https://cdn.rawgit.com/petkaantonov/bluebird/v2.3.11/js/browser/bluebird.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/q.js/1.1.1/q.min.js"></script>
<script src="https://rsvpjs-builds.s3.amazonaws.com/rsvp-latest.js"></script>
<script src="https://cdn.rawgit.com/calvinmetcalf/lie/2.7.7/dist/lie.noConflict.js"></script>
<script src="https://cdn.rawgit.com/calvinmetcalf/catiline/2.9.3/dist/catiline.js"></script>
//kew 0.5.0-alpha1
<script src="http://pastebin.com/raw.php?i=b7R6bgzk"></script>
<script src="https://cdn.rawgit.com/zeusdeux/pimp/0.2.3/browser/pimp.0.2.3.js"></script>
<script src="http://mstade.github.com/promise-jsperf/javascripts/when.js"></script>
<script src="http://www.btetrud.com/files/asyncFuture.umd.js"></script>
<script>
  window.setImmediate = _setImmediate;
  window.Promise = _Promise;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
lie
// async test
var code = 'lie' + Math.random();
var p = new Lie(function(resolve, reject) {
  function eventFunc(e) {
    if (e.data === code) {
      window.removeEventListener('message', eventFunc);
      resolve();
    }
  }
  window.addEventListener('message', eventFunc);
});


var curFuture = p
var x = 0
for(var n=0; n<1000; n++) {
  curFuture = curFuture.then(function(){
    x++
    return new Lie(function(resolve, reject) {
      resolve(x)
    })
  })
}

curFuture.then(function(){
   deferred.resolve()  
})


window.postMessage(code, '*');
ready
RSVP
// async test
var d = RSVP.defer();
var code = 'rsvp' + Math.random();

function eventFunc(e) {
  if (e.data === code) {
    window.removeEventListener('message', eventFunc);
    d.resolve();

  }
}
window.addEventListener('message', eventFunc);

var curFuture = d.promise
var x = 0
for(var n=0; n<1000; n++) {
  curFuture = curFuture.then(function(){
    x++
    var nextFuture = RSVP.defer()
    nextFuture.resolve(x)
    return nextFuture
  })
}

curFuture.then(function(){
   deferred.resolve()  
})

window.postMessage(code, '*');
ready
Native
// async test
try {
var code = 'native' + Math.random();
var p = new Promise(function(resolve, reject) {
  function eventFunc(e) {
    if (e.data === code) {
      window.removeEventListener('message', eventFunc);
      resolve();
    }
  }
  window.addEventListener('message', eventFunc);
});


var curFuture = p
var x = 0
for(var n=0; n<1000; n++) {
  curFuture = curFuture.then(function(){
    x++
    return new Promise(function(resolve, reject) {
      resolve(x)
    })
  })
}

curFuture.then(function(){
   deferred.resolve()  
})

window.postMessage(code, '*');
} catch(e) {
  // working around error handling issue https://github.com/mathiasbynens/jsperf.com/issues
  // an error will happen in IE (of course)
  // putting some code here so that the test doesn't think Native is the fastest (even if I have no way of telling jsperf the test errored)
  var n=0, callback
  setTimeout(callback=function() {
    if(n<3) {n++;setTimeout(callback,0)}
    else deferred.resolve()    
  },0)
  throw e
}
ready
when.js
// async test
var code = 'jquery' + Math.random();

var d = when.defer()
window.addEventListener('message', function eventFunc(e) {
  if (e.data === code) {
    window.removeEventListener('message', eventFunc);
    d.resolve()
  }
});

var curFuture = d.promise
var x = 0
for(var n=0; n<1000; n++) {
  curFuture = curFuture.then(function(){
    x++
    var nextFuture = when.defer()
    nextFuture.resolve(x)
    return nextFuture
  })
}

curFuture.then(function(){
   deferred.resolve()  
})

window.postMessage(code, '*');
ready
async-future
// async test
var code = 'jquery' + Math.random();

var d = new asyncFuture
window.addEventListener('message', function eventFunc(e) {
  if (e.data === code) {
    window.removeEventListener('message', eventFunc);
    d.return()
  }
});

var curFuture = d
var x = 0
for(var n=0; n<1000; n++) {
  curFuture = curFuture.then(function(){
    x++
    return asyncFuture(x) // immediately resolved future
  })
}

curFuture.then(function(){
   deferred.resolve()  
}).done()

window.postMessage(code, '*');
ready

Revisions

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