Last active
September 4, 2017 18:47
-
-
Save MarkHerhold/a03d87c01b5cad362dddc1cd60ade62b to your computer and use it in GitHub Desktop.
Generators VS Async/Await Performance
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
// | |
// Simple generator (with co) VS async/await benchmark | |
// Article: https://medium.com/p/806d8375a01a | |
// | |
const co = require('co'); | |
const Benchmark = require('benchmark'); | |
const suite = new Benchmark.Suite; | |
// add tests | |
suite | |
.add('co', { | |
defer: true, | |
fn: function(deferred) { | |
co(function*() { | |
yield Promise.resolve(1); | |
yield Promise.resolve(2); | |
// test is complete | |
deferred.resolve(); | |
}); | |
} | |
}) | |
.add('async/await', { | |
defer: true, | |
fn: function(deferred) { | |
(async function() { | |
await Promise.resolve(1); | |
await Promise.resolve(2); | |
// test is complete | |
deferred.resolve(); | |
})() | |
} | |
}) | |
// add listeners | |
.on('cycle', function(event) { | |
console.log(String(event.target)); | |
}) | |
.on('complete', function() { | |
console.log('Fastest is ' + this.filter('fastest').map('name')); | |
}) | |
// run async | |
.run({ | |
'async': false | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment