Average module. An efficient way to calculate average.
npm install simple-average
!Last version






> An efficient way to calculate average.
- 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.
``bash`
npm install simple-average --saveUsage
First, load the library:
`js`
const simpleAverage = require('simple-average')
Then create a new instance to use:
`js`
const aggregator = simpleAverage()
Now you can add new samples to calculate the average:
`js`
aggregator.add(2)
Also you can provide a n number of values to add:
`js`
average.add(2)
average.add(2, 3, 4, 5)
Methods are chainable as well:
`js`
const resume = average.add(2).add(3).add(4).add(5).resume()
console.log(resume)
// => 3.5
Create a new instance.
Reset the current counter.
Add values to calculate the average. You can provide a n` number of values or an array of values.
Get the current number of values added for calculate the average.
Get an output with the average.
MIT © Kiko Beats