diff --git a/.eslintrc.json b/.eslintrc.json index ca7217d..755381f 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,7 +1,7 @@ { "parser": "@typescript-eslint/parser", "parserOptions": { - "project": "tsconfig.json" + "project": ["tsconfig.json", "examples/tsconfig.json"] }, "plugins": [ "@typescript-eslint/eslint-plugin" diff --git a/examples/src/simple.ts b/examples/src/simple.ts index d3b6a06..71806a5 100644 --- a/examples/src/simple.ts +++ b/examples/src/simple.ts @@ -16,16 +16,40 @@ bench a = b + a; b = a - b; a = b - a; + }) + .add('async switch 1', async () => { + let a = 1; + let b = 2; + const c = a; + a = b; + b = c; + }) + .add('async switch 2', async () => { + let a = 1; + let b = 10; + a = b + a; + b = a - b; + a = b - a; }); await bench.run(); -console.table(bench.tasks.map(({ name, result }) => ({ "Task Name": name, "Average Time (ps)": result?.mean! * 1000, "Variance (ps)": result?.variance! * 1000 }))); +console.table( + bench.tasks.map(({ name, result }) => (result ? ({ + 'Task Name': name, + 'ops/sec': result.hz, + 'Average Time (ps)': result.mean * 1000, + 'Variance (ps)': result.variance * 1000, + Samples: result.samples.length, + }) : null)), +); // Output: -// ┌─────────┬────────────┬────────────────────┬────────────────────┐ -// │ (index) │ Task Name │ Average Time (ps) │ Variance (ps) │ -// ├─────────┼────────────┼────────────────────┼────────────────────┤ -// │ 0 │ 'switch 1' │ 1.8458325710527104 │ 1.2113875253341617 │ -// │ 1 │ 'switch 2' │ 1.8746935152109603 │ 1.2254725890767446 │ -// └─────────┴────────────┴────────────────────┴────────────────────┘ +// ┌─────────┬──────────────────┬────────────────────┬─────────────────────┬───────────────────────┬─────────┐ +// │ (index) │ Task Name │ ops/sec │ Average Time (ps) │ Variance (ps) │ Samples │ +// ├─────────┼──────────────────┼────────────────────┼─────────────────────┼───────────────────────┼─────────┤ +// │ 0 │ 'switch 1' │ 14463.371878123484 │ 0.06914017066190119 │ 0.006014460930767669 │ 414836 │ +// │ 1 │ 'switch 2' │ 14880.867755337544 │ 0.06720038215790977 │ 0.0004529273320957288 │ 458644 │ +// │ 2 │ 'async switch 1' │ 7970.65255150332 │ 0.1254602422497256 │ 0.010617119343817942 │ 367049 │ +// │ 3 │ 'async switch 2' │ 8702.849913513868 │ 0.11490488862127687 │ 0.002613875857497825 │ 389356 │ +// └─────────┴──────────────────┴────────────────────┴─────────────────────┴───────────────────────┴─────────┘ diff --git a/package.json b/package.json index 47c3e49..084874a 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,9 @@ "version": "2.3.0", "type": "module", "packageManager": "pnpm@7.5.1", + "engines": { + "node": ">= 10.0.0" + }, "scripts": { "dev": "tsup --watch", "build": "tsup", diff --git a/src/task.ts b/src/task.ts index b08d7b9..7dbe5f4 100644 --- a/src/task.ts +++ b/src/task.ts @@ -1,10 +1,11 @@ import type { Fn, TaskEvents, TaskResult, TaskEventsMap, } from 'types/index'; +import { types } from 'util'; import Bench from './bench'; import tTable from './constants'; import { createBenchEvent } from './event'; -import { getMean, getVariance } from './utils'; +import { getMean, getSum, getVariance } from './utils'; /** * A class that represents each benchmark task in Tinybench. It keeps track of the @@ -55,20 +56,22 @@ export default class Task extends EventTarget { && !this.bench.signal?.aborted ) { let taskStart = 0; + const isAsync = types.isAsyncFunction(this.fn); try { - // eslint-disable-next-line no-await-in-loop - await Promise.resolve().then(() => { - taskStart = this.bench.now(); - return this.fn(); - }); + taskStart = this.bench.now(); + if (isAsync) { + await this.fn(); + } else { + this.fn(); + } + const taskTime = this.bench.now() - taskStart; + this.runs += 1; + samples.push(taskTime); } catch (e) { this.setResult({ error: e }); } - const taskTime = this.bench.now() - taskStart; - this.runs += 1; - samples.push(taskTime); totalTime = this.bench.now() - startTime; } await this.bench.teardown(this, 'run'); @@ -78,7 +81,7 @@ export default class Task extends EventTarget { { const min = samples[0]!; const max = samples[samples.length - 1]!; - const period = totalTime / this.runs; + const period = getSum(samples) / this.runs; const hz = 1 / period; // benchmark.js: https://github.com/bestiejs/benchmark.js/blob/42f3b732bac3640eddb3ae5f50e445f3141016fd/benchmark.js#L1912-L1927 diff --git a/src/utils.ts b/src/utils.ts index 5f49ba2..62c6e5b 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -19,3 +19,8 @@ export const getVariance = (samples: number[], mean: number) => { const result = samples.reduce((sum, n) => sum + ((n - mean) ** 2)); return result / (samples.length - 1) || 0; }; + +/** + * Computes the sum of a sample + */ +export const getSum = (samples: number[]) => samples.reduce((sum, n) => sum + n, 0);