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 工作原理的第六章. 现在 ...
随机推荐
- HDU 5617 Jam's maze dp+滚动数组
题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5617 bc(中文):http://bestcoder.hdu.edu.cn/contest ...
- android入门 — Service
Service完全在后台运行,没有用户界面.使用的时候先创建Service子类,然后在AndroidManifest.xml中进行注册,同时可以通过<intent-filter.../>进 ...
- C#控制台应用程序
使用C#创建控制台应用程序的基本步骤: (1)创建项目: (2)编辑C#源代码: (3)编译运行: 例题:在控制台输出“Hello world!”. 第一步:文件→新建→项目:选择“项目类型”为Vis ...
- lintcode395-硬币排成线 II
395-硬币排成线 II 有 n 个不同价值的硬币排成一条线.两个参赛者轮流从左边依次拿走 1 或 2 个硬币,直到没有硬币为止.计算两个人分别拿到的硬币总价值,价值高的人获胜. 请判定 第一个玩家 ...
- iOS- Swift:使用FMDB进行数据库操作(线程安全:增删改查)
1.前言 GitHub上2000多颗星的FMDB数据库框架想来大家都很熟悉, 今天用Swift对其进行了一个完成的数据存储读流程 写完之后用博客分享之,与大家一起交流, 希望对需要的朋友提供些帮助 ...
- PAT 甲级 1005 Spell It Right
https://pintia.cn/problem-sets/994805342720868352/problems/994805519074574336 Given a non-negative i ...
- 主流的RPC框架有哪些
RPC是远程过程调用的简称,广泛应用在大规模分布式应用中,作用是有助于系统的垂直拆分,使系统更易拓展.Java中的RPC框架比较多,各有特色,广泛使用的有RMI.Hessian.Dubbo等.RPC还 ...
- MySQL专题 1 分布式部署数据库同步问题 BinLog
什么是 Binlog MySQL Server 有四种类型的日志——Error Log.General Query Log.Binary Log 和 Slow Query Log. 第一个是错误日志, ...
- Delphi中的ADOquery 用法以及ADOquery的自有方法Append 和Delete和 Edit 和Post
Delphi在数据库操作是非常好用的,delphi把ADO一些方法属性都集成了,以下是我的一些总结:*******************************通过sql的存储过程来实现:添加Wit ...
- TClientDataSet[5]: 读取数据
本例用到: TClientDataSet.Fields[]; { 字段集合; 它比 FieldList 有更多功能, 如可获取嵌套字段 } TClientDataSet.FieldL ...