Memory-efficient module that tracks the average value of an unlimited quantity of numbers
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib
test
.editorconfig
.eslintrc.js
.gitattributes
.gitignore
.travis.yml
LICENSE
README.md
bower.json
package-lock.json
package.json

README.md

running-average NPM version Bower Version Build Status Dependency Status

Memory-efficient module that tracks the average value of an unlimited quantity of numbers

Install

$ npm install --save running-average

or

$ bower install approximate-number

Usage

var RunningAverage = require('running-average'); // or use window.RunningAverage in a browser w/out Require.js or Browserify

// create an instance
var runningAverage = new RunningAverage();

// push a number
runningAverage.push(1);

// or a few numbers
runningAverage.push(2, 3, 4);

// or an array of numbers
runningAverage.push([5, 6, 7]);

// get the current average any time you want
runningAverage.getAverage(); // => 4

// add some more numbers
runningAverage.push(8, 9);

// get an updated average 
runningAverage.getAverage(); // => 5

// oh, and it's chainable too!
runningAverage.push(10).push(11).getAverage(); // => 6

Also see version 1.0 for a windowSize option to limit the average to the last n numbers, but be aware that it uses a less efficient algorithm.

License

MIT © Nathan Friedly