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