Giter Club home page Giter Club logo

live-moving-average's Introduction

live-moving-average

An ever updating average of the last n values. See Moving Average.

npm version build status ISC-licensed minimum Node.js version chat with me on Gitter support me via GitHub Sponsors

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 = 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) / 3

avg.push(5)
avg.push(6)
avg.get() // 5, because (4 + 5 + 6) / 3

avg.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.

live-moving-average's People

Contributors

derhuerst avatar wayneparrott avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

wayneparrott

live-moving-average's Issues

Make push() fluent

This for this useful package. This is an enhancement request to make the push() function fluent.

example:

movingAvg.
  push(100).
  get();

I'll submit a PR with this small change for your consideration.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.