| 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(e => { |
| 27 | console.error(e) |
| 28 | }) |
| 29 | }), |
| 30 | Benchmark.add('voided promise', () => { |
| 31 | // eslint-disable-next-line no-void |
| 32 | void asyncFunction() |
| 33 | }), |
| 34 | Benchmark.add('mishandled promise', () => { |
| 35 | asyncFunction() |
| 36 | }), |
| 37 | Benchmark.cycle(), |
| 38 | Benchmark.complete(), |
| 39 | Benchmark.save({ |
| 40 | file: 'promise-handling', |
| 41 | format: 'json', |
| 42 | details: true |
| 43 | }), |
| 44 | Benchmark.save({ |
| 45 | file: 'promise-handling', |
| 46 | format: 'chart.html', |
| 47 | details: true |
| 48 | }), |
| 49 | Benchmark.save({ |
| 50 | file: 'promise-handling', |
| 51 | format: 'table.html', |
| 52 | details: true |
| 53 | }) |
| 54 | ).catch(err => { |
| 55 | console.error(err) |
| 56 | }) |