Created
September 18, 2018 11:41
-
-
Save YSMull/bc97a0dd62bd3148df087d9b73e563b8 to your computer and use it in GitHub Desktop.
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
const co = require('co') | |
var count = 975; | |
let test_co = co.wrap(function* (n) { | |
if (n == count) | |
return; | |
yield test_co(n + 1); | |
}); | |
async function test_async(n) { | |
if (n == count) | |
return; | |
await test_async(n + 1); | |
} | |
function test_callback(n, cb) { | |
if (n == count) { | |
return cb(); | |
} else { | |
process.nextTick(() =>test_callback(n + 1, cb)); | |
} | |
} | |
function test_sync(n) { | |
if (n == count) | |
return; | |
test_sync(n + 1); | |
} | |
function test_promise(n) { | |
return new Promise((resolve, reject) => { | |
const next = (num) => { | |
if (num == count) { | |
resolve(); | |
} else { | |
new Promise((res, rej) => { | |
res(); | |
}).then(next(num + 1)); | |
} | |
} | |
next(n); | |
}) | |
} | |
async function test() { | |
let asyncfn = async () => { | |
console.time("async"); | |
await test_async(0); | |
console.timeEnd("async"); | |
}; | |
let cofn = async () => { | |
console.time("co"); | |
await test_co(0); | |
console.timeEnd("co"); | |
} | |
let callbakcfn = async () => { | |
console.time("callback"); | |
test_callback(0, () => { | |
console.timeEnd("callback"); | |
}); | |
} | |
let syncfn = async () => { | |
console.time("sync"); | |
test_sync(0); | |
console.timeEnd("sync"); | |
} | |
let promisefn = async () => { | |
console.time("promise"); | |
test_promise(0).then((res)=>{ | |
console.timeEnd("promise"); | |
}); | |
} | |
await asyncfn(); | |
await cofn(); | |
await callbakcfn(); | |
await syncfn(); | |
await promisefn(); | |
// await syncfn(); | |
// await promisefn(); | |
// await asyncfn(); | |
// await callbakcfn(); | |
// await cofn(); | |
} | |
test(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment