| 1 | import Benchmark from 'benny' |
| 2 | |
| 3 | /** |
| 4 | * |
| 5 | */ |
| 6 | async function asyncFunction () { |
| 7 | await new Promise(resolve => { |
| 8 | resolve() |
| 9 | }) |
| 10 | } |
| 11 | |
| 12 | Benchmark.suite( |
| 13 | 'Promise handling', |
| 14 | Benchmark.add('await promise', async () => { |
| 15 | try { |
| 16 | return await asyncFunction() |
| 17 | } catch (e) { |
| 18 | console.error(e) |
| 19 | } |
| 20 | }), |
| 21 | Benchmark.add('promise with then().catch()', () => { |
| 22 | asyncFunction() |
| 23 | .then(r => { |
| 24 | return r |
| 25 | }) |
| 26 | .catch(console.error) |
| 27 | }), |
| 28 | Benchmark.add('voided promise', () => { |
| 29 | // eslint-disable-next-line no-void |
| 30 | void asyncFunction() |
| 31 | }), |
| 32 | Benchmark.add('mishandled promise', () => { |
| 33 | asyncFunction() |
| 34 | }), |
| 35 | Benchmark.cycle(), |
| 36 | Benchmark.complete(), |
| 37 | Benchmark.save({ |
| 38 | file: 'promise-handling', |
| 39 | format: 'json', |
| 40 | details: true |
| 41 | }), |
| 42 | Benchmark.save({ |
| 43 | file: 'promise-handling', |
| 44 | format: 'chart.html', |
| 45 | details: true |
| 46 | }), |
| 47 | Benchmark.save({ |
| 48 | file: 'promise-handling', |
| 49 | format: 'table.html', |
| 50 | details: true |
| 51 | }) |
| 52 | ).catch(console.error) |