Cleanup object generator API
[benchmarks-js.git] / promise-handling.js
CommitLineData
7e861288 1const Benchmark = require('benny')
ed2968f2 2
e9bfc28e
JB
3/**
4 *
5 */
ed2968f2 6function promise () {
7e861288
JB
7 return new Promise(resolve => {
8 resolve()
9 })
ed2968f2
JB
10}
11
e9bfc28e
JB
12/**
13 *
14 */
ed2968f2 15async function asyncFunction () {
ce26b710 16 return await promise()
ed2968f2
JB
17}
18
7e861288
JB
19Benchmark.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 }),
37 Benchmark.add('mishandled promise', () => {
ed2968f2 38 asyncFunction()
7e861288
JB
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
ed2968f2 56 })
7e861288 57)