JavaScript Promises 实现框架一览及对比
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 | cho45@lowreal.net | 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:

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:

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
link: http://www.cnblogs.com/oooweb/p/javascript-promises-a-comparison-of-libraries.html
JavaScript Promises 实现框架一览及对比的更多相关文章
- Vue与React两个框架的区别对比
简单介绍 React--Facebook创建的JavaScript UI框架.它支撑着包括Instagram在内的大多数Facebook网站.React与当时流行的jQuery,Backbone.js ...
- JavaScript客户端MVC 框架综述
简介 15 年前,许多人都使用 Perl 和 ColdFusion 之类的工具构建网站.我们经常编写可以在页面顶部查询数据库的脚本,对数据应用必要的转换,以及在同一个脚本底部显示数据.这类架构适合于向 ...
- Atitit.js javascript的rpc框架选型
Atitit.js javascript的rpc框架选型 1. Dwr1 2. 使用AJAXRPC1 2.2. 数据类型映射表1 3. json-rpc轻量级远程调用协议介绍及使用2 3.1. 2.3 ...
- javascript获取iframe框架中页面document对象,获取子页面里面的内容,iframe获取父页面的元素,
javascript获取iframe框架中,加载的页面document对象 因为浏览器安全限制,对跨域访问的页面,其document对象无法读取.设置属性 function getDocument(i ...
- CSS+Javascript的那些框架
CSS CSS 制作框架 SASS http://www.oschina.net/p/sass Blueprint http://www.oschina.net/p/blueprintcss Ela ...
- JavaScript ES6 核心功能一览(转)
原文地址:Overview of JavaScript ES6 features (a.k.a ECMAScript 6 and ES2015+) 原文作者:Adrian Mejia 译文出自:掘金翻 ...
- JavaScript跳出iframe框架
一.window.top top属性返回最顶层的先辈窗口. 该属性返回对一个顶级窗口的只读引用.如果窗口本身就是一个顶级窗口,top属性存放对窗口自身的引用.如果窗口是一个框架,那么top属性引用包含 ...
- NodeJS框架一览
NodeJS 框架一览 Express 当你使用Node.js构建web应用程序时, Express通常被视为事实上的Web服务器.它的哲学(一个可以使用中间件包扩展的简约核心)是大多数Node.js ...
- JavaScript 工作原理之六-WebAssembly 对比 JavaScript 及其使用场景
原文请查阅这里,略有改动,本文采用知识共享署名 4.0 国际许可协议共享,BY Troland. 本系列持续更新中,Github 地址请查阅这里. 这是 JavaScript 工作原理的第六章. 现在 ...
随机推荐
- week1:个人博客作业
1.软件工程课程的希望和目标 老师步置的任务完整的做完,每一步都是自己做的,明白自己做的每一步,和为什么这样做. 期末考试最后为95分以上,最好是100. 每周学习这门课时间 每周2节课(90分钟)+ ...
- Maven实现项目构建直接部署Web项目到Tomcat
Maven实现项目构建直接部署Web项目到Tomcat配置如下: 1.Tomcat的用户及权限配置:在conf目录下,找到tomcat-users.xml,添加manager权限的用户. <ro ...
- 解决Max retries exceeded with url的问题
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='itunes.apple.com', port=443): Max ret ...
- django里的http协议
一个普通的user Begin########## ['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__' ...
- list的迭代器能解决并发问题,collection 的迭代器不能解决并发问题,for可以解决并发问题
list的迭代器能解决并发问题,collection 的迭代器不能解决并发问题 为什么list支持add,collection不支持 例如有两个人同时添加第三个元素 list的迭代器能锁定线程 只有等 ...
- hdfs源码分析第二弹
以写文件为例,串联整个流程的源码: FSDataOutputStream out = fs.create(outFile); 1. DistributedFileSystem 继承并实现了FileSy ...
- 【JavaScript】JAVA-js中比较日期大小
业务场景:js中根据yyyy-MM-dd格式的日期进行比较来动态显示相关图标的出现与否 var DS173305 = { DS173305Grid: null, initDataGrid: funct ...
- 我为什么鼓励工程师写blog
文/JoeyChen 工程师该怎样才能突破自己的能力瓶颈?写 blog! 工程师该怎样精进自己在职涯上所需要的能力?写 blog! 工程师该怎样才能保持学习与成长的动能?写 blog! 工程师该怎样才 ...
- MySQL事务提交过程
一.MySQL事务提交过程(一) MySQL作为一种关系型数据库,已被广泛应用到互联网中的诸多项目中.今天我们来讨论下事务的提交过程. 由于mysql插件式存储架构,导致开启binlog后,事务提交实 ...
- [codeforces464D]World of Darkraft - 2 概率期望
D. World of Darkraft - 2 time limit per test 2 seconds memory limit per test 256 megabytes input sta ...