promise-performance

JavaScript performance comparison

Test case created by xcodebuild

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  !function(n,e){"object"==typeof exports&&"undefined"!=typeof module?e():"function"==typeof define&&define.amd?define(e):e()}(0,function(){"use strict";var n=setTimeout;function e(n){return Boolean(n&&void 0!==n.length)}function t(){}function r(n){if(!(this instanceof r))throw new TypeError("Promises must be constructed via new");if("function"!=typeof n)throw new TypeError("not a function");this._state=0,this._handled=!1,this._value=void 0,this._deferreds=[],f(n,this)}function o(n,e){for(;3===n._state;)n=n._value;0!==n._state?(n._handled=!0,r._immediateFn(function(){var t=1===n._state?e.onFulfilled:e.onRejected;if(null!==t){var r;try{r=t(n._value)}catch(n){return void u(e.promise,n)}i(e.promise,r)}else(1===n._state?i:u)(e.promise,n._value)})):n._deferreds.push(e)}function i(n,e){try{if(e===n)throw new TypeError("A promise cannot be resolved with itself.");if(e&&("object"==typeof e||"function"==typeof e)){var t=e.then;if(e instanceof r)return n._state=3,n._value=e,void c(n);if("function"==typeof t)return void f((o=t,i=e,function(){o.apply(i,arguments)}),n)}n._state=1,n._value=e,c(n)}catch(e){u(n,e)}var o,i}function u(n,e){n._state=2,n._value=e,c(n)}function c(n){2===n._state&&0===n._deferreds.length&&r._immediateFn(function(){n._handled||r._unhandledRejectionFn(n._value)});for(var e=0,t=n._deferreds.length;e<t;e++)o(n,n._deferreds[e]);n._deferreds=null}function f(n,e){var t=!1;try{n(function(n){t||(t=!0,i(e,n))},function(n){t||(t=!0,u(e,n))})}catch(n){if(t)return;t=!0,u(e,n)}}r.prototype.catch=function(n){return this.then(null,n)},r.prototype.then=function(n,e){var r=new this.constructor(t);return o(this,new function(n,e,t){this.onFulfilled="function"==typeof n?n:null,this.onRejected="function"==typeof e?e:null,this.promise=t}(n,e,r)),r},r.prototype.finally=function(n){var e=this.constructor;return this.then(function(t){return e.resolve(n()).then(function(){return t})},function(t){return e.resolve(n()).then(function(){return e.reject(t)})})},r.all=function(n){return new r(function(t,r){if(!e(n))return r(new TypeError("Promise.all accepts an array"));var o=Array.prototype.slice.call(n);if(0===o.length)return t([]);var i=o.length;function u(n,e){try{if(e&&("object"==typeof e||"function"==typeof e)){var c=e.then;if("function"==typeof c)return void c.call(e,function(e){u(n,e)},r)}o[n]=e,0==--i&&t(o)}catch(n){r(n)}}for(var c=0;c<o.length;c++)u(c,o[c])})},r.resolve=function(n){return n&&"object"==typeof n&&n.constructor===r?n:new r(function(e){e(n)})},r.reject=function(n){return new r(function(e,t){t(n)})},r.race=function(n){return new r(function(t,o){if(!e(n))return o(new TypeError("Promise.race accepts an array"));for(var i=0,u=n.length;i<u;i++)r.resolve(n[i]).then(t,o)})},r._immediateFn="function"==typeof setImmediate&&function(n){setImmediate(n)}||function(e){n(e,0)},r._unhandledRejectionFn=function(n){"undefined"!=typeof console&&console&&console.warn("Possible Unhandled Promise Rejection:",n)};!function(){if("undefined"!=typeof self)return self;if("undefined"!=typeof window)return window;if("undefined"!=typeof global)return global;throw new Error("unable to locate global object")}();window.PromisePolyfill=r}),function(n,e){"object"==typeof exports&&"undefined"!=typeof module?e():"function"==typeof define&&define.amd?define(e):e()}(0,function(){"use strict";setTimeout;function n(n){return Boolean(n&&void 0!==n.length)}function e(){}function t(n){if(!(this instanceof t))throw new TypeError("Promises must be constructed via new");if("function"!=typeof n)throw new TypeError("not a function");this._state=0,this._handled=!1,this._value=void 0,this._deferreds=[],c(n,this)}function r(n,e){for(;3===n._state;)n=n._value;0!==n._state?(n._handled=!0,t._immediateFn(function(){var t=1===n._state?e.onFulfilled:e.onRejected;if(null!==t){var r;try{r=t(n._value)}catch(n){return void i(e.promise,n)}o(e.promise,r)}else(1===n._state?o:i)(e.promise,n._value)})):n._deferreds.push(e)}function o(n,e){try{if(e===n)throw new TypeError("A promise cannot be resolved with itself.");if(e&&("object"==typeof e||"function"==typeof e)){var r=e.then;if(e instanceof t)return n._state=3,n._value=e,void u(n);if("function"==typeof r)return void c((o=r,f=e,function(){o.apply(f,arguments)}),n)}n._state=1,n._value=e,u(n)}catch(e){i(n,e)}var o,f}function i(n,e){n._state=2,n._value=e,u(n)}function u(n){2===n._state&&0===n._deferreds.length&&t._immediateFn(function(){n._handled||t._unhandledRejectionFn(n._value)});for(var e=0,o=n._deferreds.length;e<o;e++)r(n,n._deferreds[e]);n._deferreds=null}function c(n,e){var t=!1;try{n(function(n){t||(t=!0,o(e,n))},function(n){t||(t=!0,i(e,n))})}catch(n){if(t)return;t=!0,i(e,n)}}t.prototype.catch=function(n){return this.then(null,n)},t.prototype.then=function(n,t){var o=new this.constructor(e);return r(this,new function(n,e,t){this.onFulfilled="function"==typeof n?n:null,this.onRejected="function"==typeof e?e:null,this.promise=t}(n,t,o)),o},t.prototype.finally=function(n){var e=this.constructor;return this.then(function(t){return e.resolve(n()).then(function(){return t})},function(t){return e.resolve(n()).then(function(){return e.reject(t)})})},t.all=function(e){return new t(function(t,r){if(!n(e))return r(new TypeError("Promise.all accepts an array"));var o=Array.prototype.slice.call(e);if(0===o.length)return t([]);var i=o.length;function u(n,e){try{if(e&&("object"==typeof e||"function"==typeof e)){var c=e.then;if("function"==typeof c)return void c.call(e,function(e){u(n,e)},r)}o[n]=e,0==--i&&t(o)}catch(n){r(n)}}for(var c=0;c<o.length;c++)u(c,o[c])})},t.resolve=function(n){return n&&"object"==typeof n&&n.constructor===t?n:new t(function(e){e(n)})},t.reject=function(n){return new t(function(e,t){t(n)})},t.race=function(e){return new t(function(r,o){if(!n(e))return o(new TypeError("Promise.race accepts an array"));for(var i=0,u=e.length;i<u;i++)t.resolve(e[i]).then(r,o)})};let f;f=(n=>{let e=1;const t=new MutationObserver(n),r=document.createTextNode(String(e));t.observe(r,{characterData:!0}),e=(e+1)%2,r.data=String(e)}),t._immediateFn=f,t._unhandledRejectionFn=function(n){"undefined"!=typeof console&&console&&console.warn("Possible Unhandled Promise Rejection:",n)};!function(){if("undefined"!=typeof self)return self;if("undefined"!=typeof window)return window;if("undefined"!=typeof global)return global;throw new Error("unable to locate global object")}();window.PromisePolyfillDOMObserver=t}),function(n,e){"object"==typeof exports&&"undefined"!=typeof module?e():"function"==typeof define&&define.amd?define(e):e()}(0,function(){"use strict";setTimeout;function n(n){return Boolean(n&&void 0!==n.length)}function e(){}function t(n){if(!(this instanceof t))throw new TypeError("Promises must be constructed via new");if("function"!=typeof n)throw new TypeError("not a function");this._state=0,this._handled=!1,this._value=void 0,this._deferreds=[],c(n,this)}function r(n,e){for(;3===n._state;)n=n._value;0!==n._state?(n._handled=!0,t._immediateFn(function(){var t=1===n._state?e.onFulfilled:e.onRejected;if(null!==t){var r;try{r=t(n._value)}catch(n){return void i(e.promise,n)}o(e.promise,r)}else(1===n._state?o:i)(e.promise,n._value)})):n._deferreds.push(e)}function o(n,e){try{if(e===n)throw new TypeError("A promise cannot be resolved with itself.");if(e&&("object"==typeof e||"function"==typeof e)){var r=e.then;if(e instanceof t)return n._state=3,n._value=e,void u(n);if("function"==typeof r)return void c((o=r,f=e,function(){o.apply(f,arguments)}),n)}n._state=1,n._value=e,u(n)}catch(e){i(n,e)}var o,f}function i(n,e){n._state=2,n._value=e,u(n)}function u(n){2===n._state&&0===n._deferreds.length&&t._immediateFn(function(){n._handled||t._unhandledRejectionFn(n._value)});for(var e=0,o=n._deferreds.length;e<o;e++)r(n,n._deferreds[e]);n._deferreds=null}function c(n,e){var t=!1;try{n(function(n){t||(t=!0,o(e,n))},function(n){t||(t=!0,i(e,n))})}catch(n){if(t)return;t=!0,i(e,n)}}t.prototype.catch=function(n){return this.then(null,n)},t.prototype.then=function(n,t){var o=new this.constructor(e);return r(this,new function(n,e,t){this.onFulfilled="function"==typeof n?n:null,this.onRejected="function"==typeof e?e:null,this.promise=t}(n,t,o)),o},t.prototype.finally=function(n){var e=this.constructor;return this.then(function(t){return e.resolve(n()).then(function(){return t})},function(t){return e.resolve(n()).then(function(){return e.reject(t)})})},t.all=function(e){return new t(function(t,r){if(!n(e))return r(new TypeError("Promise.all accepts an array"));var o=Array.prototype.slice.call(e);if(0===o.length)return t([]);var i=o.length;function u(n,e){try{if(e&&("object"==typeof e||"function"==typeof e)){var c=e.then;if("function"==typeof c)return void c.call(e,function(e){u(n,e)},r)}o[n]=e,0==--i&&t(o)}catch(n){r(n)}}for(var c=0;c<o.length;c++)u(c,o[c])})},t.resolve=function(n){return n&&"object"==typeof n&&n.constructor===t?n:new t(function(e){e(n)})},t.reject=function(n){return new t(function(e,t){t(n)})},t.race=function(e){return new t(function(r,o){if(!n(e))return o(new TypeError("Promise.race accepts an array"));for(var i=0,u=e.length;i<u;i++)t.resolve(e[i]).then(r,o)})};const f=[];let a,l=!1;let s=1;const d=new MutationObserver(function(){l=!1;const n=f.slice(0);f.length=0;for(let e=0;e<n.length;e++)n[e]()}),h=document.createTextNode(String(s));d.observe(h,{characterData:!0}),a=(()=>{s=(s+1)%2,h.data=String(s)}),t._immediateFn=function(n,e){let r;if(f.push(()=>{if(n)try{n.call(e)}catch(n){console.error(n)}else r&&r(e)}),l||(l=!0,a()),!n)return new t(n=>{r=n})},t._unhandledRejectionFn=function(n){"undefined"!=typeof console&&console&&console.warn("Possible Unhandled Promise Rejection:",n)};!function(){if("undefined"!=typeof self)return self;if("undefined"!=typeof window)return window;if("undefined"!=typeof global)return global;throw new Error("unable to locate global object")}();window.PromisePolyfillVueNextTick=t}),function(n){(function(n){function e(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return n[r].call(o.exports,o,o.exports,e),o.l=!0,o.exports}var t={};e.m=n,e.c=t,e.i=function(n){return n},e.d=function(n,t,r){e.o(n,t)||Object.defineProperty(n,t,{configurable:!1,enumerable:!0,get:r})},e.n=function(n){var t=n&&n.__esModule?function(){return n.default}:function(){return n};return e.d(t,"a",t),t},e.o=function(n,e){return Object.prototype.hasOwnProperty.call(n,e)},e.p="",e(e.s=100)})({100:
  /*!***********************!*\
    !*** ./src/global.js ***!
    \***********************/
  function(n,e,t){(function(n){var e=t(/*! ./yaku */5);try{n.Promise=e,window.PromisePolyfillService=e}catch(n){}}).call(e,t(/*! ./../~/webpack/buildin/global.js */2))},2:
  /*!***********************************!*\
    !*** (webpack)/buildin/global.js ***!
    \***********************************/
  function(n,e){var t;t=function(){return this}();try{t=t||Function("return this")()||(0,eval)("this")}catch(n){"object"==typeof window&&(t=window)}n.exports=t},5:
  /*!*********************!*\
    !*** ./src/yaku.js ***!
    \*********************/
  function(e,t,r){(function(t){!function(){"use strict";function r(){return Z[B][N]||H}function o(n){return n&&"object"==typeof n}function i(n){return"function"==typeof n}function u(n,e){return n instanceof e}function c(n,e,t){if(!e(n))throw d(t)}function f(){try{return F.apply(x,arguments)}catch(n){return Q.e=n,Q}}function a(n,e){return F=n,x=e,f}function l(n,e){function t(){for(var t=0;t<o;)e(r[t],r[t+1]),r[t++]=T,r[t++]=T;o=0,r.length>n&&(r.length=n)}var r=A(n),o=0;return function(n,e){r[o++]=n,r[o++]=e,2===o&&Z.nextTick(t)}}function s(n,e){var t,r,o,c,f=0;if(!n)throw d(q);var l=n[Z[B][D]];if(i(l))r=l.call(n);else{if(!i(n.next)){if(u(n,A)){for(t=n.length;f<t;)e(n[f],f++);return f}throw d(q)}r=n}for(;!(o=r.next()).done;)if((c=a(e)(o.value,f++))===Q)throw i(r[L])&&r[L](),c.e;return f}function d(n){return new TypeError(n)}function h(n){return(n?"":z)+(new C).stack}function p(n,e){var t="on"+n.toLowerCase(),r=R[t];k&&k.listeners(n).length?n===K?k.emit(n,e._v,e):k.emit(n,e):r?r({reason:e._v,promise:e}):Z[n](e._v,e)}function v(n){return n&&n._s}function y(n){return v(n)?new n(W):(e=new n(function(n,o){if(e)throw d();t=n,r=o}),c(t,i),c(r,i),e);var e,t,r}function _(n,e){var t=!1;return function(r){t||(t=!0,O&&(n[$]=h(!0)),e===M?j(n,r):b(n,e,r))}}function w(n,e){function t(n){return r.push(n.replace(/^\s+|\s+$/g,""))}var r=[];return O&&(e[$]&&t(e[$]),function n(e){e&&Y in e&&(n(e._next),t(e[Y]+""),n(e._p))}(e)),(n&&n.stack?n.stack:n)+("\n"+r.join("\n")).replace(X,"")}function m(n,e){return n(e)}function b(n,e,t){var r=0,o=n._c;if(n._s===U)for(n._s=e,n._v=t,e===I&&(O&&function(n){return u(n,C)}(t)&&(t.longStack=w(t,n)),en(n));r<o;)nn(n,n[r++]);return n}function j(n,e){if(e===n&&e)return b(n,I,d(G)),n;if(e!==E&&(i(e)||o(e))){var t=a(g)(e);if(t===Q)return b(n,I,t.e),n;i(t)?(O&&v(e)&&(n._next=e),v(e)?P(n,e,t):Z.nextTick(function(){P(n,e,t)})):b(n,M,e)}else b(n,M,e);return n}function g(n){return n.then}function P(n,e,t){var r=a(t,e)(function(t){e&&(e=E,j(n,t))},function(t){e&&(e=E,b(n,I,t))});r===Q&&e&&(b(n,I,r.e),e=E)}var T,F,x,E=null,R="object"==typeof self?self:t,k=(R.Promise,R.process),S=R.console,O=!1,A=Array,C=Error,I=1,M=2,U=3,B="Symbol",D="iterator",N="species",H=B+"("+N+")",L="return",V="_uh",Y="_pt",$="_st",q="Invalid argument",z="\nFrom previous ",G="Chaining cycle detected for promise",J="rejectionHandled",K="unhandledRejection",Q={e:E},W=function(){},X=/^.+\/node_modules\/yaku\/.+\n?/gm,Z=function(n){var e,t=this;if(!o(t)||t._s!==T)throw d("Invalid this");if(t._s=U,O&&(t[Y]=h()),n!==W){if(!i(n))throw d(q);(e=a(n)(_(t,M),_(t,I)))===Q&&b(t,I,e.e)}};Z.default=Z,function(n,e){for(var t in e)n[t]=e[t]}(Z.prototype,{then:function(e,t){if(this._s===n)throw d();return function(n,e,t,r){return i(t)&&(e._onFulfilled=t),i(r)&&(n[V]&&p(J,n),e._onRejected=r),O&&(e._p=n),n[n._c++]=e,n._s!==U&&nn(n,e),e}(this,y(Z.speciesConstructor(this,Z)),e,t)},catch:function(n){return this.then(T,n)},finally:function(n){return this.then(function(e){return Z.resolve(n()).then(function(){return e})},function(e){return Z.resolve(n()).then(function(){throw e})})},_c:0,_p:E}),Z.resolve=function(n){return v(n)?n:j(y(this),n)},Z.reject=function(n){return b(y(this),I,n)},Z.race=function(n){var e=this,t=y(e),r=function(n){b(t,M,n)},o=function(n){b(t,I,n)},i=a(s)(n,function(n){e.resolve(n).then(r,o)});return i===Q?e.reject(i.e):t},Z.all=function(n){function e(n){b(o,I,n)}var t,r=this,o=y(r),i=[];return(t=a(s)(n,function(n,u){r.resolve(n).then(function(n){i[u]=n,--t||b(o,M,i)},e)}))===Q?r.reject(t.e):(t||b(o,M,[]),o)},Z.Symbol=R[B]||{},a(function(){Object.defineProperty(Z,r(),{get:function(){return this}})})(),Z.speciesConstructor=function(n,e){var t=n.constructor;return t&&t[r()]||e},Z.unhandledRejection=function(n,e){S&&S.error("Uncaught (in promise)",O?e.longStack:w(n,e))},Z.rejectionHandled=W,Z.enableLongStackTrace=function(){O=!0},Z.nextTick=function(n){setTimeout(n)},Z._s=1;var nn=l(999,function(n,e){var t,r;return(r=n._s!==I?e._onFulfilled:e._onRejected)===T?void b(e,n._s,n._v):(t=a(m)(r,n._v))===Q?void b(e,I,t.e):void j(e,t)}),en=l(9,function(n){(function n(e){if(e._umark)return!0;e._umark=!0;for(var t,r=0,o=e._c;r<o;)if((t=e[r++])._onRejected||n(t))return!0})(n)||(n[V]=1,p(K,n))});try{e.exports=Z}catch(n){R.Yaku=Z}}()}).call(t,r(/*! ./../~/webpack/buildin/global.js */2))}})}.call("object"==typeof window&&window||"object"==typeof self&&self||"object"==typeof global&&global||{});

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
NativePromise
// async test
function A() {
  return new Promise((res, rej) => {
    res('test')
  })
}

A().then(result => {
  console.log('test');
  deferred.resolve()
})
pending…
PromisePolyfillSetTimeout
// async test
function A() {
  return new PromisePolyfill((res, rej) => {
    res('test')
  })
}

A().then(result => {
  console.log('test');
  deferred.resolve()
})
pending…
PromisePolyfillDOMObserver
// async test
function A() {
  return new PromisePolyfillDOMObserver((res, rej) => {
    res('test')
  })
}

A().then(result => {
  console.log('test');
  deferred.resolve()
})
pending…
PromiseVueNextTick
// async test
function A() {
  return new PromisePolyfillVueNextTick((res, rej) => {
    res('test')
  })
}

A().then(result => {
  console.log('test');
  deferred.resolve()
})
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 Comments