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.

Revisions

  1. chrisyip created this gist Jul 14, 2017.
    39 changes: 39 additions & 0 deletions ac.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    '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)
    })
    })