X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=promise-handling.js;h=11e0a96493056430a470792173f8fd4fd50f04d9;hb=52d07fb863422041a24874efb893c711bf0b9cd4;hp=e4e712b2f664e25e736d91d35d3876bb022f969d;hpb=e2bfb54955f0d0138b5c83a918c96112a5710d52;p=benchmarks-js.git diff --git a/promise-handling.js b/promise-handling.js index e4e712b..11e0a96 100644 --- a/promise-handling.js +++ b/promise-handling.js @@ -1,37 +1,63 @@ -const Benchmark = require('benchmark') -const { LIST_FORMATTER } = require('./benchmark-utils') - -const suite = new Benchmark.Suite() +const Benchmark = require('benny') /** * */ function promise () { - return new Promise() + return new Promise(resolve => { + resolve() + }) } /** * */ async function asyncFunction () { - await promise() + return await promise() } -suite - .add('await promise', async () => { - await asyncFunction() - }) - .add('promise', () => { +Benchmark.suite( + 'Promise handling', + Benchmark.add('await promise', async () => { + try { + return await asyncFunction() + } catch (e) { + console.error(e) + } + }), + Benchmark.add('promise with then().catch()', () => { asyncFunction() + .then(r => { + return r + }) + .catch(e => { + console.error(e) + }) + }), + Benchmark.add('voided promise', () => { + // eslint-disable-next-line no-void + void asyncFunction() + }), + Benchmark.add('mishandled promise', () => { + asyncFunction() + }), + Benchmark.cycle(), + Benchmark.complete(), + Benchmark.save({ + file: 'promise-handling', + format: 'json', + details: true + }), + Benchmark.save({ + file: 'promise-handling', + format: 'chart.html', + details: true + }), + Benchmark.save({ + file: 'promise-handling', + format: 'table.html', + details: true }) - .on('cycle', event => { - console.log(event.target.toString()) - }) - .on('complete', function () { - console.log( - 'Fastest is ' + LIST_FORMATTER.format(this.filter('fastest').map('name')) - ) - // eslint-disable-next-line n/no-process-exit - process.exit() - }) - .run() +).catch(err => { + console.error(err) +})