Skip to content

Instantly share code, notes, and snippets.

@stelcheck
Last active April 13, 2017 05:38
Show Gist options
  • Save stelcheck/b20d9a2ba4ed2b5169cadeaaa8da0b5f to your computer and use it in GitHub Desktop.
Save stelcheck/b20d9a2ba4ed2b5169cadeaaa8da0b5f to your computer and use it in GitHub Desktop.
es-promisify benchmark
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