Created
November 20, 2017 16:53
-
-
Save medatech/f77d15e7ececd120501069d9434ace79 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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