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 |
With node v10.8.0
on my local machine MacBook Pro 2016, 2,9 GHz Intel Core i5:
co generators x 279,442 ops/sec ±3.21% (64 runs sampled)
async/await x 408,913 ops/sec ±20.58% (61 runs sampled)
native promises x 2,652,819 ops/sec ±6.96% (63 runs sampled)
bluebird promises x 1,936,632 ops/sec ±8.21% (64 runs sampled)
'Fastest is native promises'
co generators x 532,403 ops/sec ±2.10% (80 runs sampled)
async/await x 730,501 ops/sec ±1.03% (78 runs sampled)
native promises x 4,403,051 ops/sec ±1.01% (81 runs sampled)
bluebird promises x 1,607,709 ops/sec ±0.70% (56 runs sampled)
Fastest is native promises
$ node -v
v11.12.0
'co generators x 436,833 ops/sec ±1.71% (78 runs sampled)'
'async/await x 4,597,435 ops/sec ±0.69% (86 runs sampled)'
'native promises x 5,108,470 ops/sec ±1.20% (84 runs sampled)'
'bluebird promises x 3,126,262 ops/sec ±0.90% (87 runs sampled)'
'Fastest is native promises'
$ node -v
v12.1.0
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
Hi~ what is your node.js version?
btw, "Fastest is native promises" in node v10.8.0