diff --git a/packages/benchmark.js-plugin/src/index.ts b/packages/benchmark.js-plugin/src/index.ts index 3abece7..8d7a8a5 100644 --- a/packages/benchmark.js-plugin/src/index.ts +++ b/packages/benchmark.js-plugin/src/index.ts @@ -177,16 +177,21 @@ async function runBenchmarks({ } else { benchPayload = bench.fn as CallableFunction; } + if (isAsync) { await optimizeFunction(benchPayload); - measurement.startInstrumentation(); - await benchPayload(); - measurement.stopInstrumentation(uri); + await (async function __codspeed_root_frame__() { + measurement.startInstrumentation(); + await benchPayload(); + measurement.stopInstrumentation(uri); + })(); } else { optimizeFunctionSync(benchPayload); - measurement.startInstrumentation(); - benchPayload(); - measurement.stopInstrumentation(uri); + (function __codspeed_root_frame__() { + measurement.startInstrumentation(); + benchPayload(); + measurement.stopInstrumentation(uri); + })(); } console.log(` ✔ Measured ${uri}`); benchmarkCompletedListeners.forEach((listener) => listener()); diff --git a/packages/tinybench-plugin/src/index.ts b/packages/tinybench-plugin/src/index.ts index 422e498..e884630 100644 --- a/packages/tinybench-plugin/src/index.ts +++ b/packages/tinybench-plugin/src/index.ts @@ -24,14 +24,12 @@ export function withCodSpeed(bench: Bench): Bench { console.log(`[CodSpeed] running with @codspeed/tinybench v${__VERSION__}`); for (const task of bench.tasks) { const uri = callingFile + "::" + task.name; - // eslint-disable-next-line no-inner-declarations - async function __codspeed_root_frame__() { + await optimizeFunction(task.fn()); + await (async function __codspeed_root_frame__() { + measurement.startInstrumentation(); await task.fn(); - } - await optimizeFunction(__codspeed_root_frame__); - measurement.startInstrumentation(); - await __codspeed_root_frame__(); - measurement.stopInstrumentation(uri); + measurement.stopInstrumentation(uri); + })(); console.log(` ✔ Measured ${uri}`); } console.log(`[CodSpeed] Done running ${bench.tasks.length} benches.`);