Skip to content

Commit

Permalink
fix: remove dirtyAsyncCheck option and implement
Browse files Browse the repository at this point in the history
  • Loading branch information
kainstar authored and Aslemammad committed Oct 18, 2022
1 parent 74c5c0c commit 6bcb3bd
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 29 deletions.
5 changes: 1 addition & 4 deletions src/bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
} from 'types/index';
import { createBenchEvent } from './event';
import Task from './task';
import { isAsyncFunction, isAsyncFunctionDirty, now } from './utils';
import { now } from './utils';

/**
* The Benchmark instance for keeping track of the benchmark tasks and controlling
Expand All @@ -32,8 +32,6 @@ export default class Bench extends EventTarget {

now = now;

isAsyncFunction: (fn: Fn) => boolean;

setup: Hook;

teardown: Hook;
Expand All @@ -44,7 +42,6 @@ export default class Bench extends EventTarget {
this.warmupTime = options.warmupTime ?? this.warmupTime;
this.warmupIterations = options.warmupIterations ?? this.warmupIterations;
this.time = options.time ?? this.time;
this.isAsyncFunction = options.dirtyAsyncCheck ? isAsyncFunctionDirty : isAsyncFunction;
this.iterations = options.iterations ?? this.iterations;
this.signal = options.signal;
// eslint-disable-next-line @typescript-eslint/no-empty-function
Expand Down
4 changes: 2 additions & 2 deletions src/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
import Bench from './bench';
import tTable from './constants';
import { createBenchEvent } from './event';
import { getMean, getVariance } from './utils';
import { getMean, getVariance, isAsyncFunction } from './utils';

/**
* A class that represents each benchmark task in Tinybench. It keeps track of the
Expand Down Expand Up @@ -47,7 +47,7 @@ export default class Task extends EventTarget {
this.dispatchEvent(createBenchEvent('start', this));
let totalTime = 0; // ms
const samples: number[] = [];
const isAsync = this.bench.isAsyncFunction(this.fn);
const isAsync = isAsyncFunction(this.fn);

await this.bench.setup(this, 'run');
while (
Expand Down
13 changes: 0 additions & 13 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,3 @@ const AsyncFunctionConstructor = (async () => {}).constructor;
* an async function check method only consider runtime support async syntax
*/
export const isAsyncFunction = (fn: Fn) => fn.constructor === AsyncFunctionConstructor;

/**
* an async function check method consider runtime not support async syntax
*/
export const isAsyncFunctionDirty = (fn: Fn) => {
try {
const ret = fn();
return !!ret && typeof ret === 'object' && typeof ret.then === 'function';
} catch {
// if fn throw error directly, consider it's a sync function
return false;
}
};
10 changes: 0 additions & 10 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,6 @@ export type Options = {
*/
now?: () => number;

/**
* decide use which way to check task's fn is a async function.
*
* when it's false, only compare function's constructor
* when is's true, run task's fn and check whether return a PromiseLike object
*
* @default false
*/
dirtyAsyncCheck?: boolean;

/**
* An AbortSignal for aborting the benchmark
*/
Expand Down

0 comments on commit 6bcb3bd

Please sign in to comment.