live-moving-average
An ever updating average of the last n values. See Moving Average.
Reuses the values array to prevent GC-thrashing. Does not slow down with an increasing window size:
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
npm install live-moving-average
Usage
const createWindow = const avg = // size of 3, fill with 0 // using api fluentlyavg // 3, because (2 + 3 + 4) / 3 avgavgavg // 5, because (4 + 5 + 6) / 3 avgavg // 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.