Skip to content

Commit f10f65a

Browse files
committed
fixup! feat: add t-test mode for statistical significance testing
1 parent 8742dd1 commit f10f65a

File tree

6 files changed

+52
-12
lines changed

6 files changed

+52
-12
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -726,8 +726,8 @@ Example output:
726726
```
727727
T-Test Mode: Enabled (repeatSuite=30)
728728
729-
baseline x 1,234,567 ops/sec (300 runs sampled) min..max=(...)
730-
optimized x 9,876,543 ops/sec (305 runs sampled) min..max=(...)
729+
baseline x 1,234,567 ops/sec (300 runs sampled) min..max=(810.05ns...812.45ns)
730+
optimized x 9,876,543 ops/sec (305 runs sampled) min..max=(101.23ns...102.87ns)
731731
732732
Summary (vs. baseline):
733733
baseline (baseline)

lib/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const {
3030
validateString,
3131
validateArray,
3232
validateBenchmarkMode,
33+
validateBoolean,
3334
} = require("./validators");
3435
const {
3536
welchTTest,
@@ -152,7 +153,10 @@ class Suite {
152153
printHeader: true,
153154
};
154155

155-
this.#ttest = options.ttest || false;
156+
if (options.ttest !== undefined) {
157+
validateBoolean(options.ttest, "options.ttest");
158+
}
159+
this.#ttest = options.ttest ?? false;
156160

157161
let repeatSuite = defaultBenchOptions.repeatSuite;
158162
if (options.repeatSuite !== undefined) {

lib/reporter/chart.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,18 @@ function toChart(results, options = { labelWidth: 45, printHeader: true }) {
130130
if (hasBaseline) {
131131
if (result.baseline) {
132132
comment = styleText("magenta", "(baseline)");
133-
} else if (result.comparison.startsWith("-")) {
134-
comment = styleText("red", `(${result.comparison.slice(1)}x slower)`);
135133
} else {
136-
comment = styleText("green", `(${result.comparison}x faster)`);
134+
const isSignificant = result.significanceTest?.significant ?? true;
135+
const isFaster = !result.comparison.startsWith("-");
136+
const comparisonText = isFaster
137+
? `(${result.comparison}x faster)`
138+
: `(${result.comparison.slice(1)}x slower)`;
139+
140+
if (isSignificant) {
141+
comment = styleText(isFaster ? "green" : "red", comparisonText);
142+
} else {
143+
comment = styleText("dim", comparisonText);
144+
}
137145
}
138146
}
139147

lib/reporter/pretty.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,18 @@ function toPretty(results, options = {}) {
7474

7575
if (result.baseline) {
7676
text += styleText("magenta", "(baseline)");
77-
} else if (!result.comparison.startsWith("-")) {
78-
text += styleText("green", `(${result.comparison}x faster)`);
7977
} else {
80-
text += styleText("red", `(${result.comparison.slice(1)}x slower)`);
78+
const isSignificant = result.significanceTest?.significant ?? true;
79+
const isFaster = !result.comparison.startsWith("-");
80+
const comparisonText = isFaster
81+
? `(${result.comparison}x faster)`
82+
: `(${result.comparison.slice(1)}x slower)`;
83+
84+
if (isSignificant) {
85+
text += styleText(isFaster ? "green" : "red", comparisonText);
86+
} else {
87+
text += styleText("dim", comparisonText);
88+
}
8189
}
8290

8391
if (result.significanceTest) {

lib/reporter/text.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,16 @@ function toText(results, options = {}) {
7373
if (result.baseline) {
7474
text += styleText("magenta", "(baseline)");
7575
} else if (result.comparison !== undefined) {
76-
if (!result.comparison.startsWith("-")) {
77-
text += styleText("green", `(${result.comparison}x faster)`);
76+
const isSignificant = result.significanceTest?.significant ?? true;
77+
const isFaster = !result.comparison.startsWith("-");
78+
const comparisonText = isFaster
79+
? `(${result.comparison}x faster)`
80+
: `(${result.comparison.slice(1)}x slower)`;
81+
82+
if (isSignificant) {
83+
text += styleText(isFaster ? "green" : "red", comparisonText);
7884
} else {
79-
text += styleText("red", `(${result.comparison.slice(1)}x slower)`);
85+
text += styleText("dim", comparisonText);
8086
}
8187

8288
if (result.significanceTest) {

lib/validators.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,25 @@ function validateArray(value, name) {
121121
);
122122
}
123123

124+
/**
125+
* Validates that a value is a boolean
126+
* @param {any} value - The value to validate
127+
* @param {string} name - Name of the parameter being validated
128+
* @throws {Error} If validation fails
129+
*/
130+
function validateBoolean(value, name) {
131+
if (typeof value !== "boolean")
132+
throw ERR_INVALID_ARG_TYPE(
133+
`value must be a boolean, name: ${name}, value: ${value}`,
134+
);
135+
}
136+
124137
module.exports = {
125138
validateFunction,
126139
validateNumber,
127140
validateObject,
128141
validateString,
129142
validateArray,
130143
validateBenchmarkMode,
144+
validateBoolean,
131145
};

0 commit comments

Comments
 (0)