simple-average
An efficient way to calculate average.
Why?
- Abstract the process of calculate the average in a independent module.
- Calculate it using a efficient memory way.
- Simple and Chainable API.
I was looking for a better solution than store all values in an array, sum all and divide for the length. This module is inspired in Daniel Bernier blog that's provide a memory efficient solution for the problem.
Install
npm install simple-average --save
Usage
First, load the library:
const simpleAverage =
Then create a new instance to use:
const aggregator =
Now you can add new samples to calculate the average:
aggregator
Also you can provide a n
number of values to add:
averageaverage
Methods are chainable as well:
const resume = averageconsole// => 3.5
API
.simpleAverage()
Create a new instance.
.reset()
Reset the current counter.
.add(values... {Number})
Add values to calculate the average. You can provide a n
number of values or an array of values.
.count()
Get the current number of values added for calculate the average.
.resume()
Get an output with the average.
License
MIT © Kiko Beats