Commit | Line | Data |
---|---|---|
f522d7b9 | 1 | import Benchmark from 'benny' |
ed2968f2 | 2 | |
e9bfc28e JB |
3 | /** |
4 | * | |
5 | */ | |
ed2968f2 | 6 | function promise () { |
7e861288 JB |
7 | return new Promise(resolve => { |
8 | resolve() | |
9 | }) | |
ed2968f2 JB |
10 | } |
11 | ||
e9bfc28e JB |
12 | /** |
13 | * | |
14 | */ | |
ed2968f2 | 15 | async function asyncFunction () { |
ce26b710 | 16 | return await promise() |
ed2968f2 JB |
17 | } |
18 | ||
7e861288 JB |
19 | Benchmark.suite( |
20 | 'Promise handling', | |
21 | Benchmark.add('await promise', async () => { | |
57f62217 JB |
22 | try { |
23 | return await asyncFunction() | |
24 | } catch (e) { | |
25 | console.error(e) | |
26 | } | |
7e861288 | 27 | }), |
57f62217 JB |
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 | }), | |
1f89b386 JB |
37 | Benchmark.add('voided promise', () => { |
38 | // eslint-disable-next-line no-void | |
39 | void asyncFunction() | |
40 | }), | |
57f62217 | 41 | Benchmark.add('mishandled promise', () => { |
ed2968f2 | 42 | asyncFunction() |
7e861288 JB |
43 | }), |
44 | Benchmark.cycle(), | |
45 | Benchmark.complete(), | |
46 | Benchmark.save({ | |
47 | file: 'promise-handling', | |
48 | format: 'json', | |
49 | details: true | |
50 | }), | |
51 | Benchmark.save({ | |
52 | file: 'promise-handling', | |
53 | format: 'chart.html', | |
54 | details: true | |
55 | }), | |
56 | Benchmark.save({ | |
57 | file: 'promise-handling', | |
58 | format: 'table.html', | |
59 | details: true | |
ed2968f2 | 60 | }) |
4b16770a JB |
61 | ).catch(err => { |
62 | console.error(err) | |
63 | }) |