Created
July 14, 2017 04:20
-
-
Save chrisyip/22bb23b77069a49d06ab84981851592c to your computer and use it in GitHub Desktop.
async vs co
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
'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