https://github.com/derhuerst/live-moving-average
An ever updating average of the last n values.
https://github.com/derhuerst/live-moving-average
average mean moving rolling window
Last synced: 6 months ago
JSON representation
An ever updating average of the last n values.
- Host: GitHub
- URL: https://github.com/derhuerst/live-moving-average
- Owner: derhuerst
- License: isc
- Created: 2017-09-10T14:14:16.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2022-02-13T16:35:14.000Z (over 3 years ago)
- Last Synced: 2025-04-15T05:17:22.705Z (6 months ago)
- Topics: average, mean, moving, rolling, window
- Language: JavaScript
- Homepage: https://github.com/derhuerst/live-moving-average
- Size: 14.6 KB
- Stars: 5
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license.md
Awesome Lists containing this project
README
# live-moving-average
**An ever updating average of the last n values.** See [Moving Average](https://en.wikipedia.org/wiki/Moving_average).
[](https://www.npmjs.com/package/live-moving-average)
[](https://travis-ci.org/derhuerst/live-moving-average)


[](https://gitter.im/derhuerst)
[](https://github.com/sponsors/derhuerst)Reuses the values array to prevent GC-thrashing. Does not slow down with an increasing window size:
```window size 10, 1 push, 1 get x 65,514,574 ops/sec ±0.62% (90 runs sampled)
window size 10, 1 push, 10 get x 49,988,485 ops/sec ±0.28% (93 runs sampled)
window size 1000, 1 push, 1 get x 64,048,288 ops/sec ±0.42% (93 runs sampled)
window size 100, 1 push, 10 get x 49,012,149 ops/sec ±0.40% (95 runs sampled)
```## Installing
```shell
npm install live-moving-average
```## Usage
```js
const createWindow = require('live-moving-average')const avg = createWindow(3, 0) // size of 3, fill with 0
// using api fluently
avg
.push(2)
.push(3)
.push(4)
.get() // 3, because (2 + 3 + 4) / 3avg.push(5)
avg.push(6)
avg.get() // 5, because (4 + 5 + 6) / 3avg.push(7)
avg.get() // 6, because (5 + 6 + 7) / 3
```## API
### `const w = createWindow(size, fill = 0)`
`size` and `fill` must be a number.
### `w.push(val)`
`val` must be a number.
Returns `w`.### `w.get()`
Returns the moving average.
## Contributing
If you have a question or need support using `live-moving-average`, please double-check your code and setup first. If you think you have found a bug or want to propose a feature, use [the issues page](https://github.com/derhuerst/live-moving-average/issues).