Skip to content

Commit

Permalink
fix: avoid duplicate benchmark run
Browse files Browse the repository at this point in the history
Signed-off-by: Jérôme Benoit <[email protected]>
  • Loading branch information
jerome-benoit committed Oct 17, 2024
1 parent 4d44e37 commit 61c8eb2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
25 changes: 14 additions & 11 deletions src/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ import {
} from './reporter/terminal/index.js'
import { runtime } from './runtime.js'
import { now } from './time.js'
import { isAsyncFnResource, isFunction, isObject } from './utils.js'
import {
isAsyncFnResource,
isAsyncFunction,
isFunction,
isObject,
} from './utils.js'

let groupName = null
const groups = new Map()
Expand Down Expand Up @@ -75,6 +80,10 @@ export function group(name, cb = undefined) {
}
if (!isFunction(cb))
throw new TypeError(`expected function, got ${cb.constructor.name}`)
if (isAsyncFunction(cb))
throw new TypeError(
`expected synchronous function, got asynchronous function ${cb.constructor.name}`
)
if (isObject(name)) {
if (name.name != null && 'string' !== typeof name.name)
throw new TypeError(
Expand Down Expand Up @@ -127,14 +136,8 @@ export function group(name, cb = undefined) {
before: name.before ?? emptyFunction,
after: name.after ?? emptyFunction,
})
if (isAsyncFnResource(cb)) {
cb().then(() => {
groupName = null
})
} else {
cb()
groupName = null
}
cb()
groupName = null
}

/**
Expand Down Expand Up @@ -362,7 +365,7 @@ export async function run(opts = {}) {
if (once) log('')
log(groupHeader(groupName, opts))
}
isAsyncFnResource(groupOpts.before)
isAsyncFunction(groupOpts.before)
? await groupOpts.before()
: groupOpts.before()

Expand All @@ -372,7 +375,7 @@ export async function run(opts = {}) {
opts,
groupOpts
)
isAsyncFnResource(groupOpts.after)
isAsyncFunction(groupOpts.after)
? await groupOpts.after()
: groupOpts.after()
}
Expand Down
9 changes: 3 additions & 6 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@ export type GroupOptions = {
after?: () => void | Promise<void>
}

export function group(cb: () => void | Promise<void>): void
export function group(name: string, cb: () => void | Promise<void>): void
export function group(
options: GroupOptions,
cb: () => void | Promise<void>
): void
export function group(cb: () => void): void
export function group(name: string, cb: () => void): void
export function group(options: GroupOptions, cb: () => void): void

export type BenchmarkOptions = {
samples?: number // minimum number of samples
Expand Down
5 changes: 3 additions & 2 deletions src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
AsyncFunction,
checkDividend,
isAsyncFnResource,
isAsyncFunction,
isFunction,
isObject,
} from './utils.js'
Expand Down Expand Up @@ -216,8 +217,8 @@ export async function measure(fn, opts = {}) {
opts.before = opts.before ?? emptyFunction
opts.after = opts.after ?? emptyFunction

const asyncBefore = isAsyncFnResource(opts.before)
const asyncAfter = isAsyncFnResource(opts.after)
const asyncBefore = isAsyncFunction(opts.before)
const asyncAfter = isAsyncFunction(opts.after)

const asyncFunction = opts.async || asyncBefore || asyncAfter

Expand Down
4 changes: 2 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const AsyncFunction = (async () => {}).constructor
* @param {Function} fn the function to check
* @returns {Boolean} true if the function is an async function
*/
const isAsyncFunction = fn => {
export const isAsyncFunction = fn => {
return AsyncFunction === fn?.constructor
}

Expand All @@ -49,7 +49,7 @@ export const isAsyncFnResource = fn => {
const promiseLike = isPromiseLike(fnCall)
if (promiseLike) {
// silence promise rejection
fnCall.catch(() => {})
fnCall.then(() => {}).catch(() => {})
}
return promiseLike
} catch {
Expand Down

0 comments on commit 61c8eb2

Please sign in to comment.