Created
September 25, 2020 11:07
-
-
Save naugtur/3c677d6be5a9dac5a620401a3e0c627c to your computer and use it in GitHub Desktop.
rolling average
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function rollingAverage(samples = 100) { | |
const avgHistory = []; | |
let avgSum = 0; | |
return { | |
add(value) { | |
avgHistory.push(value); | |
avgSum += value; | |
if (avgHistory.length > samples) { | |
const bye = avgHistory.shift(); | |
avgSum -= bye; | |
} | |
}, | |
get() { | |
return avgSum / avgHistory.length; | |
}, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment