Skip to content

Instantly share code, notes, and snippets.

@MarkHerhold
Last active September 4, 2017 18:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MarkHerhold/a03d87c01b5cad362dddc1cd60ade62b to your computer and use it in GitHub Desktop.
Save MarkHerhold/a03d87c01b5cad362dddc1cd60ade62b to your computer and use it in GitHub Desktop.
Generators VS Async/Await Performance
//
// 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