Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 32 additions & 27 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,31 +141,49 @@ class Suite {
}

this.#useWorkers = options.useWorkers || false;
this.#benchmarkMode = options.benchmarkMode || "ops";
validateBenchmarkMode(this.#benchmarkMode, "options.benchmarkMode");

// DCE detection is opt-in to avoid breaking changes
const dceEnabled = options.detectDeadCodeElimination === true;
if (dceEnabled) {
this.#dceDetector = new DeadCodeEliminationDetectionPlugin(
options.dceThreshold ? { threshold: options.dceThreshold } : {},
if (this.#useWorkers && this.#benchmarkMode === "time") {
console.warn(
"Warning: Worker mode currently doesn't fully support 'time' benchmarkMode.",
);
}

// Plugin setup: If DCE detection is enabled, default to no plugins (allow optimization)
// Otherwise, use V8NeverOptimizePlugin as the default
// DCE detection is opt-in to avoid breaking changes
let dceEnabled = false;

if (options.detectDeadCodeElimination === true) {
if (this.#useWorkers) {
console.warn(
"Warning: Worker mode currently doesn't support detectDeadCodeElimination=true.",
);
} else {
dceEnabled = true;
}
}

let plugins = [];

if (options?.plugins) {
validateArray(options.plugins, "plugin");
validatePlugins(options.plugins);
this.#plugins = options.plugins;
} else if (dceEnabled) {
plugins = [...options.plugins];
} else if (!dceEnabled) {
// DCE detection requires optimization to be enabled, so no default plugins
this.#plugins = [];
} else {
// Default behavior - use V8NeverOptimizePlugin
this.#plugins = [new V8NeverOptimizePlugin()];
plugins = [new V8NeverOptimizePlugin()];
}

this.#benchmarkMode = options.benchmarkMode || "ops";
validateBenchmarkMode(this.#benchmarkMode, "options.benchmarkMode");
if (dceEnabled) {
this.#dceDetector = new DeadCodeEliminationDetectionPlugin(
options.dceThreshold ? { threshold: options.dceThreshold } : {},
);

plugins.push(this.#dceDetector);
}

this.#plugins = plugins;

this.#reporterOptions = options.reporterOptions || {
printHeader: true,
Expand Down Expand Up @@ -278,14 +296,6 @@ class Suite {
for (let i = 0; i < this.#benchmarks.length; ++i) {
const benchmark = this.#benchmarks[i];

// Add DCE detector to benchmark plugins if enabled
if (this.#dceDetector && this.#benchmarkMode === "ops") {
const originalPlugins = benchmark.plugins;
benchmark.plugins = [...benchmark.plugins, this.#dceDetector];
// Regenerate function string with new plugins
benchmark.fnStr = createFnString(benchmark);
}

// Warmup is calculated to reduce noise/bias on the results
const initialIterations = await getInitialIterations(benchmark);
debugBench(
Expand All @@ -294,11 +304,6 @@ class Suite {

let result;
if (this.#useWorkers) {
if (this.#benchmarkMode === "time") {
console.warn(
"Warning: Worker mode currently doesn't fully support 'time' benchmarkMode.",
);
}
result = await this.runWorkerBenchmark(benchmark, initialIterations);
} else {
result = await runBenchmark(
Expand Down
Loading