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 实现框架一览及对比的更多相关文章

  1. Vue与React两个框架的区别对比

    简单介绍 React--Facebook创建的JavaScript UI框架.它支撑着包括Instagram在内的大多数Facebook网站.React与当时流行的jQuery,Backbone.js ...

  2. JavaScript客户端MVC 框架综述

    简介 15 年前,许多人都使用 Perl 和 ColdFusion 之类的工具构建网站.我们经常编写可以在页面顶部查询数据库的脚本,对数据应用必要的转换,以及在同一个脚本底部显示数据.这类架构适合于向 ...

  3. Atitit.js javascript的rpc框架选型

    Atitit.js javascript的rpc框架选型 1. Dwr1 2. 使用AJAXRPC1 2.2. 数据类型映射表1 3. json-rpc轻量级远程调用协议介绍及使用2 3.1. 2.3 ...

  4. javascript获取iframe框架中页面document对象,获取子页面里面的内容,iframe获取父页面的元素,

    javascript获取iframe框架中,加载的页面document对象 因为浏览器安全限制,对跨域访问的页面,其document对象无法读取.设置属性 function getDocument(i ...

  5. CSS+Javascript的那些框架

    CSS CSS 制作框架 SASS http://www.oschina.net/p/sass Blueprint  http://www.oschina.net/p/blueprintcss Ela ...

  6. JavaScript ES6 核心功能一览(转)

    原文地址:Overview of JavaScript ES6 features (a.k.a ECMAScript 6 and ES2015+) 原文作者:Adrian Mejia 译文出自:掘金翻 ...

  7. JavaScript跳出iframe框架

    一.window.top top属性返回最顶层的先辈窗口. 该属性返回对一个顶级窗口的只读引用.如果窗口本身就是一个顶级窗口,top属性存放对窗口自身的引用.如果窗口是一个框架,那么top属性引用包含 ...

  8. NodeJS框架一览

    NodeJS 框架一览 Express 当你使用Node.js构建web应用程序时, Express通常被视为事实上的Web服务器.它的哲学(一个可以使用中间件包扩展的简约核心)是大多数Node.js ...

  9. JavaScript 工作原理之六-WebAssembly 对比 JavaScript 及其使用场景

    原文请查阅这里,略有改动,本文采用知识共享署名 4.0 国际许可协议共享,BY Troland. 本系列持续更新中,Github 地址请查阅这里. 这是 JavaScript 工作原理的第六章. 现在 ...

随机推荐

  1. 线段树-hdu2795 Billboard(贴海报)

    hdu2795 Billboard 题意:h*w的木板,放进一些1*L的物品,求每次放空间能容纳且最上边的位子 思路:每次找到最大值的位子,然后减去L 线段树功能:query:区间求最大值的位子(直接 ...

  2. CDOJ ABCDE dp(前缀和优化)

    题目链接: http://acm.uestc.edu.cn/#/problem/show/1307 ABCDE Time Limit: 1000/1000MS (Java/Others)Memory ...

  3. Python安装Numpy,matplotlib库

    <1> Numpy是一款基于python的功能强大的科学计算包.要安装numpy首先你得先安装python. python的安装非常简单,本人安装的是python2.7 具体安装步骤如下: ...

  4. Rsyslog的三种传输协议简要介绍

    rsyslog的三种传输协议 rsyslog 可以理解为多线程增强版的syslog. rsyslog提供了三种远程传输协议,分别是: 1. UDP 传输协议 基于传统UDP协议进行远程日志传输,也是传 ...

  5. Java 成员初始化顺序

    package com.cwcec.test; class Fu { int num = 5; //构造代码块 { System.out.println("Fu constructor co ...

  6. SQL 语句(增删改查)

    一.增:有4种方法1.使用insert插入单行数据: --语法:insert [into] <表名> [列名] values <列值> 例:insert into Strden ...

  7. Hbase的安装和配置

    1,准备好hbase的linux环境下的压缩包,这里hadoop版本为hadoop2.5.0,hbase版本为 2,解压缩这个版本,不选src的,其实两个任一都行 进入到hbase安装包目录,我这里的 ...

  8. nginx & restart

    nginx & restart https://www.cyberciti.biz/faq/nginx-linux-restart/

  9. mysql 、慢查询、到底如何玩

    在项目开发中,那些开发大佬经常会写出一些SQL语句,一条糟糕的SQL语句可能让你测试的整个程序都非常慢,超过10秒的话,我觉得一般用户就会选择关闭网页,如何优化SQL语句将那些运行时间 比较长的SQL ...

  10. 【bzoj5028】小Z的加油店 扩展裴蜀定理+差分+线段树

    题目描述 给出 $n$ 个瓶子和无限的水,每个瓶子有一定的容量.每次你可以将一个瓶子装满水,或将A瓶子内的水倒入B瓶子中直到A倒空或B倒满.$m$ 次操作,每次给 $[l,r]$ 内的瓶子容量增加 $ ...