Skip to content

Instantly share code, notes, and snippets.

@chrisyip
Created July 14, 2017 04:20
Show Gist options
  • Save chrisyip/22bb23b77069a49d06ab84981851592c to your computer and use it in GitHub Desktop.
Save chrisyip/22bb23b77069a49d06ab84981851592c to your computer and use it in GitHub Desktop.
async vs co
'use strict'
console.log('Using node %s', process.versions.node)
function p () {
return Promise.resolve('hello').then(s => `${s} world`)
}
const a = async function () {
await p()
}
const co = require('co')
const c = co.wrap(function * () {
yield p()
})
suite('async vs co', function () {
bench('async', function (next) {
const ps = []
for (let index = 0; index < 100; index++) {
ps.push(a())
}
Promise.all(ps).then(next, next)
})
bench('co', function (next) {
const ps = []
for (let index = 0; index < 100; index++) {
ps.push(c())
}
Promise.all(ps).then(next, next)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment