Skip to content

Instantly share code, notes, and snippets.

@naugtur
Created September 25, 2020 11:07
Show Gist options
  • Save naugtur/3c677d6be5a9dac5a620401a3e0c627c to your computer and use it in GitHub Desktop.
Save naugtur/3c677d6be5a9dac5a620401a3e0c627c to your computer and use it in GitHub Desktop.
rolling average
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