UNPKG

@naturalcycles/js-lib

Version:

Standard library for universal (browser + Node.js) javascript

31 lines (30 loc) 615 B
/** * Container that allows to accumulate average of a set of numbers, * without the need to store all of them in memory. * * @experimental */ export class AccumulatingAverage { total = 0; count = 0; /** * Returns the current average. * Returns 0 if no values have been added. */ get average() { if (this.count === 0) return 0; return this.total / this.count; } /** * Adds a new value. */ add(value) { this.total += value; this.count++; } reset() { this.total = 0; this.count = 0; } }