running-average
v2.0.2
Published
Memory-efficient module that tracks the average value of an unlimited quantity of numbers
Downloads
5
Maintainers
Readme
running-average
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