Last active
April 13, 2017 05:38
-
-
Save stelcheck/b20d9a2ba4ed2b5169cadeaaa8da0b5f to your computer and use it in GitHub Desktop.
es-promisify benchmark
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 num = process.argv[2] | |
const promisify = require('es6-promisify'); | |
function someFunction(callback) { | |
process.nextTick(callback.bind(null, null, new Date())); | |
} | |
async function loop(call) { | |
for (let i = 0; i < num; i+= 1) { | |
await call() | |
} | |
} | |
async function testPromise() { | |
await loop(function () { | |
return new Promise(function (resolve, reject) { | |
someFunction(function (error, data) { | |
if (error) { | |
reject(error); | |
return; | |
} | |
resolve(data); | |
}) | |
}) | |
}) | |
} | |
async function testEsPromisify() { | |
await loop(function () { | |
return promisify(someFunction)(); | |
}) | |
} | |
async function time(label, call) { | |
console.time(label) | |
await call() | |
console.timeEnd(label) | |
} | |
async function run() { | |
while (true) { | |
await time('promise', testPromise) | |
await time('es-promisify', testEsPromisify) | |
} | |
} | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment