Skip to content

Instantly share code, notes, and snippets.

@Aschen
Last active February 12, 2020 02:40
Show Gist options
  • Select an option

  • Save Aschen/87cc0c0873249f5b773313470cff0ce7 to your computer and use it in GitHub Desktop.

Select an option

Save Aschen/87cc0c0873249f5b773313470cff0ce7 to your computer and use it in GitHub Desktop.
Benchmark returning promises
node bench/returning-promises.js
standard x 3,096,388 ops/sec ±0.77% (91 runs sampled)
async wrapped x 1,853,672 ops/sec ±1.12% (81 runs sampled)
await then return x 1,074,238 ops/sec ±1.96% (53 runs sampled)
Fastest is standard
'use strict'
var benchmark = require('benchmark')
var suite = new benchmark.Suite()
const total = 0;
function doPromise () {
return new Promise(resolve => {
let total = Date.now();
for (let i = 0; i < total % 50000; i++) {
total += total % i;
}
resolve(total);
});
}
function standard () {
return doPromise();
}
async function withAsync () {
return doPromise();
}
async function awaitAsync () {
const res = await doPromise();
return res;
}
suite.add('standard', function () {
return standard();
})
suite.add('async wrapped', function () {
return withAsync();
})
suite.add('await then return', function () {
return awaitAsync();
})
suite.on('cycle', function(event) {
console.log(String(event.target));
})
suite.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
suite.run({ async: true })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment