perf-insight
Version:
Performance benchmarking tool for NodeJS.
63 lines • 1.69 kB
JavaScript
export function createRunningStdDev() {
return new RunningStdDevImpl();
}
class RunningStdDevImpl {
#count = 0;
#sum = 0;
#m2 = 0;
#min = Infinity;
#max = -Infinity;
push(value) {
let mean = this.#count ? this.#sum / this.#count : 0;
this.#count++;
const delta = value - mean;
this.#sum += value;
mean = this.#sum / this.#count;
const delta2 = value - mean;
this.#m2 += delta * delta2;
this.#min = Math.min(this.#min, value);
this.#max = Math.max(this.#max, value);
}
get count() {
return this.#count;
}
/**
* Returns true if there are at least two values.
*/
get ok() {
return this.#count > 1;
}
get mean() {
assert(this.#count, 'No values to calculate mean');
return this.#sum / this.#count;
}
get variance() {
assert(this.#count > 1, 'Need at least two values to calculate variance');
return this.#m2 / this.#count;
}
get sampleVariance() {
assert(this.#count > 1, 'Need at least two values to calculate sample variance');
return this.#m2 / (this.#count - 1);
}
get standardDeviation() {
return Math.sqrt(this.variance);
}
get sampleStandardDeviation() {
return Math.sqrt(this.sampleVariance);
}
get min() {
return this.#min;
}
get max() {
return this.#max;
}
get p95() {
return this.mean + 1.96 * this.sampleStandardDeviation;
}
}
function assert(condition, message) {
if (!condition) {
throw new Error(message ?? 'Assertion failed');
}
}
//# sourceMappingURL=sd.mjs.map