Try it locally
Compute the mean, sample variance, and Welch t statistic with HazelJS Stats, simple-statistics, and jStat. Use Node.js 20 or later and an npm registry that serves HazelJS 2.2.0.
mkdir stats-example
cd stats-example
npm init -y
npm install @hazeljs/stats@2.2.0 simple-statistics@7.12.1 jstat@1.9.6
Save this as compare.mjs.
import { meanAndVariance, tests } from '@hazeljs/stats';
import { mean, sampleVariance } from 'simple-statistics';
import jstat from 'jstat';
const { jStat } = jstat;
const a = [1, 2, 3, 4];
const b = [2, 4, 6, 8];
const hazel = meanAndVariance(Float64Array.from(a));
const welch = tests.ttestIndependent(a, b, { equalVariance: false });
const jStatSampleVariance = (values) => jStat.variance(values, true);
const statistic = (meanOf, varianceOf) =>
(meanOf(a) - meanOf(b)) /
Math.sqrt(varianceOf(a) / a.length + varianceOf(b) / b.length);
console.table({
'HazelJS Stats': { ...hazel, welchT: welch.statistic },
'simple-statistics': {
mean: mean(a),
variance: sampleVariance(a),
welchT: statistic(mean, sampleVariance),
},
jStat: {
mean: jStat.mean(a),
variance: jStatSampleVariance(a),
welchT: statistic(jStat.mean, jStatSampleVariance),
},
});
console.log('Full HazelJS Welch test', {
pValue: welch.pValue,
degreesOfFreedom: welch.degreesOfFreedom,
});
node compare.mjs
All three rows should show a mean of 2.5, a sample variance of about 1.6667, and a Welch t statistic of about -1.7321. The last line prints the p-value and degrees of freedom from the full HazelJS test. Set b to [4, 3, 2, 1] to try two samples with equal means.
Head-to-head results
On an Apple M3 Max (arm64, Node.js 23.10.0), HazelJS Stats returned the lowest median of the three tested libraries for mean plus sample variance and for the Welch t statistic. On 100,000 observations, mean plus variance took about 30% less time than the next-lowest median, from jStat. Measured results JSON
| Workload | Observations | HazelJS Stats (ms) | simple-statistics (ms) | jStat (ms) |
|---|---|---|---|---|
| Mean and sample variance | 100,000 | 0.146 | 0.239 | 0.210 |
| Welch t statistic | 100,000 per sample | 0.289 | 0.453 | 0.397 |
The Welch row times the statistic alone. It uses the same formula and equivalent inputs in each library. The full test also reports the p-value, degrees of freedom, and other result fields. The harness prepares HazelJS TypedArrays and comparison-library arrays before timing.
Frame query aggregation
Both paths scanned the same 100,000-row CSV and produced a full descriptive summary. Computing the aggregates in DuckDB took about 64% less time than collecting the numeric column and running the summary in JavaScript.
| Path | Median (ms) |
|---|---|
describeFrame computes aggregates inside DuckDB | 18.979 |
collectFrameColumns followed by JavaScript describe | 52.349 |
Other Stats workloads
The harness recorded HazelJS timings for these public APIs. These rows have no third-party timing in this run. A dash in the console table marks an unmeasured combination.
| Workload | Observations or settings | HazelJS Stats (ms) |
|---|---|---|
| Full descriptive summary | 100,000 | 7.193 |
| Full Welch test | 100,000 per sample | 0.444 |
| Pearson correlation | 100,000 pairs | 0.648 |
| OLS regression | 100,000 | 33.097 |
| Binomial GLM | 10,000 | 15.159 |
| One-way ANOVA | 300,000 across three groups | 8.501 |
| Mann–Whitney U | 10,000 across two samples | 2.020 |
| Shapiro–Wilk | 60 | 0.042 |
| Percentile bootstrap | 1,024, with 199 resamples | 5.295 |
| Online statistics | 100,000 | 2.103 |
| Merge online statistics | 16 precomputed partitions | 0.003 |
| Online covariance | 100,000 pairs | 2.330 |
Reproduce this benchmark
Each row reports the median of nine warm runs. The harness verifies results outside the timed interval.
npm ci
npm run build -w @hazeljs/frame -w @hazeljs/numeric -w @hazeljs/stats
cd benchmarks/stats
npm ci
npm start -- 100000
The run writes medians, workload sizes, and runtime information to benchmarks/stats/results/latest.json. Stats package guide
FAQ
- What does a dash mean in the benchmark?
- A dash marks a library and workload combination that the benchmark did not time.
- Does the Welch comparison include p-values?
- The head-to-head row computes only the Welch t statistic. The full test, including p-values and degrees of freedom, has a separate HazelJS Stats timing.