Main image
3rd March
2014
written by

In my previous post in Danish I looked at how to perform asynchronous calls by using promises. Now the time has come to pick which library that fits the next project.

There is a lot of variants and the spread is huge. One search for promise via the node package manager npmjs.org gave 1150 libraries which either provides or are dependent on promises. Of these I have picked 12 different libraries to look at, all are open source and all offer a promise-like structure.

Updates:

  • 2014/03/06 – Fixed a few misspellings (@rauschma via Twitter)
  • 2014/03/07 – Removed raw sizes, since they did’nt make much sense (@x-skeww via Reddit)
  • 2014/03/07 – Added that catiline uses lie underneath. (@CWMma via Twitter)
  • 2014/03/07 – Added clarification on what the test does. (@CWMma via Twitter)

The API across the libraries are almost alike, so I’ve decided to look at:

Features
What kind of generic promise related features does each library offer?

Size
And I’m thinking mostly browsers here – how many extra bytes will this add to my site?

Speed
How fast are the basic promise operations in the library? You would expect that these will execute many times so this is important.

 

The libraries

First an overview of the selected candidates, their license and author. Note that the name is linking to the source of the library (typical Github).

 

License Author Note
Bluebird MIT Petka Antonov Loaded with features and should be one of the fastest around and with special empathizes on error handling via good stack traces. Features can be toggled via custom builds.
Catiline MIT Calvin Metcalf Mostly designed for handling of web workers but contains a promise implementation. Uses lie underneath.
ES6 Promise polyfill MIT Jake Archibald Borrows code from RSVP, but implemented according to the ECMAScript 6 specification.
jQuery MIT  The jQuery Foundation Classic library for DOM-manipulation across browsers.
kew Apache 2.0  The Obvious Corporation I’m guessing it is pronounced ‘Q’, can be considered as a optimized edition of Q but with a smaller feature set.
lie MIT Calvin Metcalf
MyDeferred MIT RubaXa Small Gist style implementation
MyPromise MIT [email protected] Small Gist style implementation
Q MIT Kris Kowal Well known implementation, a light edition of it can be found in the popular AngularJS framework from Google.
RSVP MIT Tilde
when MIT cujoJS
Yui BSD Yahoo! Yahoo’s library for DOM-manipulation across browsers.

 

Features

The following is a look at the library feature set, looking only at features directly linked to promises:

 

Promises/A+ Progression Delayed promise Parallel synchronization Web Workers Cancellation Generators Wrap jQuery
Bluebird ✓ (+389 B) ✓ (+615 B) ✓ (+272 B) ✓ (+396 B) ✓ (+276 B)
Catiline
ES6 Promise polyfill
JQuery
kew
lie
MyDeferred
MyPromise
Q
RSVP
when
Yui


The numbers in parenthesis by Bluebird is the additional size in bytes each feature will add.

Promises/A+
Is the Promises/A+ specification implemented?

Progression
Are methods provided for notification on status on asynchronous tasks before the task is completed?

Delayed promise
Can you create a promise that is resolved after a specified delay?

Parallel synchronization
Are there methods for synchronization of multiple operations, can we get a resolved promise when a bunch of other promises are resolved?

Web Workers
Can asynchronous code be executed via a web worker – pushed to a separate execution thread?

Cancellation
Can promise execution be stopped before it is finished?

Generators
Are coming functions around JavaScript generators supported?

Wrap jQuery
Can promises produced by jQuery be converted to this library’s promises?

 

Size

Every library have been minified via Googles Closure compiler. All executed on ‘Simple’ to prevent any damaging changes. For libraries that support custom builds I have picked the smallest configuration that still supports promises. The result is including compression in the http-stack, so its actually the raw number of bytes one would expect that the application is added when using each library:

 

Minified and compressed

Speed

The speed has been measured via the site jsPerf which gives the option to execute the same tests across a lot of different browsers and platforms including mobile and tablets. The test creates a new promise with each library and measures how much latency is imposed on execution of the asynchronous block (see more detailed explanation here). Note that the test was not created by me, but a lot of fantastic people (current version is 91). The numbers are average across platforms:

 

Operations per second

Conclusion

Over half of the worlds websites already uses jQuery. If you have worked with promises in jQuery, you quickly find that they are inadequate.
I have previously had problems with failing code that doesn’t reject the promise on error as you would expect, but where the error still bubbles up and ends up being a global browser error. The promise specification dictates that errors should be caught and the promise rejected, which is not what happens in jQuery.

So if you today have a site based on jQuery, the obvious choice is to pick one of the libraries that offers conversion from jQuery’s unsafe promises to one of the more safe kind. If size is a priority either Q or when are good suggestions, loaded with features and at a decent speed.

If you are less worried about size, Bluebird is a better choice. The modularity makes it easy to toggle features and it has a significant test suite that covers performance on a lot of other aspects than the single one covered by this post.

If performance is essential, kew is a good bet. A team has picked up Q and looked into lowering its resource requirements. This has resulted in a light weight but very fast library.

If you are looking for a more limited solution with good speed and without big libraries, the ES6 Promise polyfill is a good choice – then in the long term when the browsers catch up, the library can be removed completely.

This post is also available in Danish at QED.dk

12 Comments

  1. […] This post is also available in English at Complexitymaze.com […]

  2. azu
    04/03/2014

    Is ypromise better than YUI?
    ypromise is standalone version of YUI promises.
    https://github.com/yahoo/ypromise

  3. 08/03/2014

    @azu I’ll check it out …

  4. Esailija
    08/03/2014

    The benchmark you posted is completely irrelevant to promise use cases – how about http://spion.github.io/posts/why-i-am-switching-to-promises.html ?

  5. 09/03/2014

    If it would be interesting to you, I suggest you to check Vow (https://github.com/dfilatov/vow) library as well. It implements both Promises/A+ and DOM Promise specs and works in browser and in node.js.

  6. […] JavaScript Promises – a comparison of libraries […]

  7. pashoo
    23/04/2015

    your article is outdated. The fastest library is the bluebird. It is much more faster than the native Promises in Google Chrome

  8. muchi
    15/05/2015

    It suprises me whe all the datagrids uses jQuery if it is so slow. I was looking for one to my recent project and found http://jspreadsheets.com/ and checked in at https://atmospherejs.com/ and what should I use. Guess there is only one option to go with vanilla?

  9. 22/06/2015

    Have you looked at Promiz? It’s written by Zoli Kahan, a genius guy (he was accepted in the Thiel Fellows this month).

    https://github.com/Zolmeister/promiz

  10. […] Comparison of JavaScript Promise Libraries (Mar, 2014) […]

Leave a Reply

WordPress Appliance - Powered by TurnKey Linux