Forked from netroy/generators-vs-asyncawait-vs-native-promises.js
Last active
April 26, 2021 11:52
-
-
Save joseluisq/a87adfd46d86d82ba787919a062f6e36 to your computer and use it in GitHub Desktop.
Native Promises vs Generators vs Async/Await Performance vs Bluebird Promises
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Benchmark = require('benchmark') | |
const co = require('co') | |
const bluebird = require('bluebird') | |
const suite = new Benchmark.Suite | |
suite | |
.add('co generators', { | |
defer: true, | |
fn: deferred => { | |
co(function*() { | |
yield Promise.resolve(1) | |
yield Promise.resolve(2) | |
deferred.resolve() | |
}) | |
} | |
}) | |
.add('async/await', { | |
defer: true, | |
fn: deferred => { | |
(async () => { | |
await Promise.resolve(1) | |
await Promise.resolve(2) | |
deferred.resolve() | |
})() | |
} | |
}) | |
.add('native promises', { | |
defer: true, | |
fn: deferred => { | |
Promise.resolve(1) | |
.then(() => Promise.resolve(2)) | |
.then(() => deferred.resolve()) | |
} | |
}) | |
.add('bluebird promises', { | |
defer: true, | |
fn: deferred => { | |
bluebird.resolve(1) | |
.then(() => bluebird.resolve(2)) | |
.then(() => deferred.resolve()) | |
} | |
}) | |
.on('cycle', event => { | |
const r = event.target | |
dd(r) | |
}) | |
.on('complete', () => { | |
dd('Fastest is ' + suite.filter('fastest').map('name')) | |
}) | |
.run({ | |
'async': false | |
}) | |
function dd(obj) { | |
console.dir(obj, {colors: true}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
co generators x 186,551 ops/sec ±1.20% (76 runs sampled) | |
async/await x 431,637 ops/sec ±0.93% (82 runs sampled) | |
native promises x 1,383,370 ops/sec ±0.50% (85 runs sampled) | |
bluebird promises x 1,896,068 ops/sec ±1.99% (80 runs sampled) | |
Fastest is bluebird promises |
icebob
commented
Apr 29, 2019
•
node v13.14.0
co generators x 328,340 ops/sec ±0.63% (84 runs sampled)
async/await x 3,504,078 ops/sec ±0.68% (84 runs sampled)
native promises x 3,590,390 ops/sec ±0.45% (87 runs sampled)
bluebird promises x 1,840,102 ops/sec ±1.12% (86 runs sampled)
Fastest is native promises
node v14.16.1
co generators x 341,121 ops/sec ±1.11% (82 runs sampled)
async/await x 3,418,420 ops/sec ±0.74% (84 runs sampled)
native promises x 2,605,891 ops/sec ±2.24% (79 runs sampled)
bluebird promises x 1,559,188 ops/sec ±2.71% (85 runs sampled)
Fastest is async/await
node v15.14.0
co generators x 320,900 ops/sec ±1.58% (79 runs sampled)
async/await x 3,384,254 ops/sec ±0.99% (86 runs sampled)
native promises x 3,005,298 ops/sec ±1.28% (86 runs sampled)
bluebird promises x 1,672,506 ops/sec ±0.29% (87 runs sampled)
Fastest is async/await
node v16.0.0
co generators x 329,304 ops/sec ±1.97% (84 runs sampled)
async/await x 3,306,700 ops/sec ±1.20% (87 runs sampled)
native promises x 3,077,731 ops/sec ±0.64% (86 runs sampled)
bluebird promises x 1,559,288 ops/sec ±0.47% (87 runs sampled)
Fastest is async/await
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment