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 工作原理的第六章. 现在 ...
随机推荐
- RabbitMQ 安装,配置
1:安装 yum install -y rabbitmq-server 2:主要程序介绍 # 管理插件的程序 /usr/sbin/rabbitmq-plugins # 服务程序 /usr/sbin ...
- HDU 2162 Add ‘em
http://acm.hdu.edu.cn/showproblem.php?pid=2162 Problem Description Write a program to determine the ...
- mysql中用户和权限
用户管理 用户数据所在位置: mysql中的所有用户,都存储在系统数据库(mysql)中的user 表中——不管哪个数据库的用户,都存储在这里. 表初始内容如下: 创建用户: 形式: create ...
- mysql 、慢查询、到底如何玩
在项目开发中,那些开发大佬经常会写出一些SQL语句,一条糟糕的SQL语句可能让你测试的整个程序都非常慢,超过10秒的话,我觉得一般用户就会选择关闭网页,如何优化SQL语句将那些运行时间 比较长的SQL ...
- SQL入门之多表查询
如果查询需要针对两个或者更多个表,则在需要涉及到表的连接操作(join). 0.笛卡儿积 最简单的连接方式是直接在from子句中加入两个表,并且用join操作符隔开.形式为Table1 join Ta ...
- 秒杀多线程第二篇 多线程第一次亲密接触 CreateThread与_beginthreadex本质区别(续)
由于原作者主要写window上的线程,而我主要学习android,所以本文将分析android方面多线程. 1.Thread: public void Thread1(){ Thread a = ne ...
- vue项目 axios封装第二弹
import axios from "axios"; import qs from "qs"; import { Message, MessageBox } f ...
- C++解析(17):操作符重载
0.目录 1.操作符重载 2.完善的复数类 3.小结 1.操作符重载 下面的复数解决方案是否可行? 示例1--原有的解决方案: #include <stdio.h> class Compl ...
- Qt浅谈之总结(整理)
Qt浅谈之总结(整理) 来源 http://blog.csdn.net/taiyang1987912/article/details/32713781 一.简介 QT的一些知识点总结,方便以后查阅. ...
- QT 主窗口和子窗口相互切换示例
QT 主窗口和子窗口相互切换示例 文件列表: SubWidget.h #ifndef SUBWIDGET_H #define SUBWIDGET_H #include <QtWidgets/QW ...