rolling-average
TypeScript icon, indicating that this package has built-in type declarations

1.1.7 • Public • Published

Rolling Average

By Tyson Jones

A zero-dependency package that helps to calculate various types of numerical averages.

Installation

npm install rolling-average or pnpm install rolling-average

Usage

There are three average calculators that can be constructed with the RollingAverageFactory class.

1. Fixed Window - Calculates the average of a fixed number of samples

import { RollingAverageFactory } from "rolling-average";
const fixedWindow = RollingAverageFactory.fixedWindow(3);
fixedWindow.addSample(1);
fixedWindow.addSample(2);
fixedWindow.addSample(3);
fixedWindow.addSample(4);
console.log(fixedWindow.getAverage()); // Prints out 3

2. Time Window - Calculates the average of the samples added during a rolling time window specified in milliseconds.

import { RollingAverageFactory } from "rolling-average";
const fixedWindow = RollingAverageFactory.timeWindow(1000);
fixedWindow.addSample(1);
// Sleep for 200 ms
fixedWindow.addSample(2);
// Sleep for 200 ms
fixedWindow.addSample(3);
// Sleep for 200 ms
fixedWindow.addSample(4);
// Sleep for 600 ms
console.log(fixedWindow.getAverage()); // Prints out 3

3. Regular - Calculates the average of all samples.

import { RollingAverageFactory } from "rolling-average";
const regular = RollingAverageFactory.regular(3);
regular.addSample(1);
regular.addSample(2);
regular.addSample(3);
console.log(regular.getAverage()); // Prints out 2

Package Sidebar

Install

npm i rolling-average

Weekly Downloads

52

Version

1.1.7

License

ISC

Unpacked Size

11.4 kB

Total Files

19

Last publish

Collaborators

  • chaptersevenseeds