Use latest pnpm version and related formats
[benchmarks-js.git] / promise-handling.js
index 03a78e282d1d983d743dd12da207bdeae1b75b96..4e83dc54268f278c6d5de9d2dd29b96cf873a4d3 100644 (file)
@@ -1,37 +1,61 @@
-const Benchmark = require('benchmark')
-const { LIST_FORMATTER } = require('./benchmark-utils')
-
-const suite = new Benchmark.Suite()
+const Benchmark = require('benny')
 
 /**
  *
  */
 function promise () {
-  return new Promise()
+  return new Promise(resolve => {
+    resolve()
+  })
 }
 
 /**
  *
  */
 async function asyncFunction () {
-  await promise()
+  return await promise()
 }
 
-suite
-  .add('await promise', async function () {
-    await asyncFunction()
-  })
-  .add('promise', function () {
+Benchmark.suite(
+  'Promise handling',
+  Benchmark.add('await promise', async () => {
+    try {
+      return await asyncFunction()
+    } catch (e) {
+      console.error(e)
+    }
+  }),
+  Benchmark.add('promise with then().catch()', () => {
     asyncFunction()
+      .then(r => {
+        return r
+      })
+      .catch(e => {
+        console.error(e)
+      })
+  }),
+  Benchmark.add('voided promise', () => {
+    // eslint-disable-next-line no-void
+    void asyncFunction()
+  }),
+  Benchmark.add('mishandled promise', () => {
+    asyncFunction()
+  }),
+  Benchmark.cycle(),
+  Benchmark.complete(),
+  Benchmark.save({
+    file: 'promise-handling',
+    format: 'json',
+    details: true
+  }),
+  Benchmark.save({
+    file: 'promise-handling',
+    format: 'chart.html',
+    details: true
+  }),
+  Benchmark.save({
+    file: 'promise-handling',
+    format: 'table.html',
+    details: true
   })
-  .on('cycle', function (event) {
-    console.log(event.target.toString())
-  })
-  .on('complete', function () {
-    console.log(
-      'Fastest is ' + LIST_FORMATTER.format(this.filter('fastest').map('name'))
-    )
-    // eslint-disable-next-line no-process-exit
-    process.exit()
-  })
-  .run()
+)