Skip to content

Instantly share code, notes, and snippets.

@medatech
Created November 20, 2017 16:53
Show Gist options
  • Save medatech/f77d15e7ececd120501069d9434ace79 to your computer and use it in GitHub Desktop.
Save medatech/f77d15e7ececd120501069d9434ace79 to your computer and use it in GitHub Desktop.
const co = require('co');
const Benchmark = require('benchmark');
function asyncOp () {
return new Promise((fulfill, reject) => {
fulfill('ok');
});
}
function yieldAction () {
return new Promise((fulfill, reject) => {
co(function *() {
const result = yield asyncOp();
fulfill(result);
}).catch(function (err) {
console.log(err)
});
});
}
function asyncAction () {
return new Promise(async (fulfill, reject) => {
try {
var result = await asyncOp();
fulfill(result);
} catch (err) {
console.log(err);
}
});
}
const suite = new Benchmark.Suite;
suite.add('yieldAction#test', () => {
return yieldAction();
}).add('asyncAction#test', () => {
return asyncAction();
}).on('cycle', function(event) {
console.log(String(event.target));
}).on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
}).run({ 'async': true });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment