Skip to content

Instantly share code, notes, and snippets.

@evan-moon
Last active August 11, 2019 09:42
Show Gist options
  • Save evan-moon/1d2a10347fffc2363c03d3422f4ad5b9 to your computer and use it in GitHub Desktop.
Save evan-moon/1d2a10347fffc2363c03d3422f4ad5b9 to your computer and use it in GitHub Desktop.
let avg = 0;
const numbers = [];
function average (numbers = []) {
const sum = numbers.reduce((prev, current) => prev + current, 0);
return sum/numbers.length;
}
console.time('avg1');
for (let i = 1; i < 100001; i++) {
numbers.push(i);
avg = average(numbers);
}
console.timeEnd('avg1');
console.log(`그래서 평균은? -> ${avg}`);
let avg2 = 0;
function cumulativeAverage (prevAvg, newNumber, listLength) {
const oldWeight = (listLength - 1) / listLength;
const newWeight = 1 / listLength;
return (prevAvg * oldWeight) + (newNumber * newWeight);
}
console.time('avg2');
for (let i = 1; i < 1000001; i++) {
avg2 = cumulativeAverage(avg2, i, i);
}
console.timeEnd('avg2');
console.log(`그래서 평균은? -> ${avg2}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment