feat: add async option (#371) · tinylibs/tinybench@f61b1ee

@@ -111,6 +111,40 @@ import { hrtimeNow } from 'tinybench'

111111112112

It may make your benchmarks slower.

113113114+

## Async Detection

115+116+

Tinybench automatically detects if a task function is asynchronous by

117+

checking if provided function is an `AsyncFunction` or if it returns a

118+

`Promise`, by calling the provided function once.

119+120+

You can also explicitly set the `async` option to `true` or `false` when adding

121+

a task, thus avoiding the detection. This can be useful, for example, for

122+

functions that return a `Promise` but are actually synchronous.

123+124+

```ts

125+

const bench = new Bench()

126+127+

bench.add('asyncTask', async () => {

128+

}, { async: true })

129+130+

bench.add('syncTask', () => {

131+

}, { async: false })

132+133+

bench.add('syncTaskReturningPromiseAsAsync', () => {

134+

return Promise.resolve()

135+

}, { async: true })

136+137+

bench.add('syncTaskReturningPromiseAsSync', () => {

138+

// for example running sync logic, which blocks the event loop anyway

139+

// like fs.writeFileSync

140+141+

// returns promise maybe for API compatibility

142+

return Promise.resolve()

143+

}, { async: false })

144+145+

await bench.run()

146+

```

147+114148

## Concurrency

115149116150

- When `mode` is set to `null` (default), concurrency is disabled.