build: bump node volta version
[benchmarks-js.git] / promise-handling.mjs
1 import Benchmark from 'benny'
2
3 /**
4 *
5 */
6 async function promise () {
7 return await 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('voided promise', () => {
38 // eslint-disable-next-line no-void
39 void asyncFunction()
40 }),
41 Benchmark.add('mishandled promise', () => {
42 asyncFunction()
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
60 })
61 ).catch(err => {
62 console.error(err)
63 })