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 |
intech
commented
Apr 26, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment