https://www.npmjs.com/package/average
var average = require('average');
var result = average([2, 5, 0, 1, 25, 7, 3, 0, 0, 10]);
console.log('The average for all the values is:', result);
It's hard to not stare at that in complete disbelief; someone thought that it was worthwhile to create a package for determining the mean of an array of numbers.[1] - https://en.wikipedia.org/wiki/Kahan_summation_algorithm
The problem with a naive average like this is that if you're averaging a bunch of big numbers there is a very high chance of overflow when you're summing, so even if all the numbers fit in 32 or 53 bits your calculation will be off.
If you're not averaging thousands of large numbers, why are you even using a package instead of nums.reduce((a,b)=>a+b) / nums.length anyway?
JavaScript has suffered from a lack of a standard library for a while. Having a small package like this means that (in theory) everyone is using the same bug free version of summing instead of writing their own.
Honestly having JS start building a standard library at this point would be wonderful.
For example: https://www.npmjs.com/package/lodash.mean
Or, in light of `left-pad`: https://www.npmjs.com/package/lodash.padstart
You get the benefit of a "small, focused" module but still rely on a large and stable project with a great maintainer that cares about performance and testability.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
If you need something more convenient I highly recommend just adding lodash and getting a whole bag of rock-solid awesome tools.
And they won't let you have fixed-point in JavaScript!
https://github.com/bytespider/average/blob/master/src/averag...
and here is the code copied out in full
module.exports = function average(values) { for (var i = 0, sum = 0; i < values.length; ++i) { sum += values[i]; } return sum / values.length; };
is this worth adding a dependency to your build for?
It's going to be damn hard to make an average function sound impressive.
Someone who understands what NPM is, has written and published working code (even stupid code) is miles ahead of the curve already.
Most companies in the world, especially non-software companies, don't go for 5% hacker gurus. Attracting someone vaguely competent is already a blessing.
I can easily see this grow into a function accepting options indicating such things as "null values are zeroes", "ignore null values", "evaluate string arguments", etc, or maybe into a factory that produces customized averaging functions.
For example, average([null,1,2,3],[4,5]) could either be 3 (the average of all numbers), 2.5 (sum of numbers divided by number of 'root' items), undefined (input format error), 3.25 (average of average of [1,2,3] and average of [4,5]), etc.
And what if it gets passed arrays of equal size? "average([1,2,3],[7,4,5])" could easily be interpreted as a vector average, and produce [4,3,4]
Silly? Yes, but if you want to write a robust library in JavaScript, you have to think about what to do with all these kinds of input.
And of course, there are the simple variants of harmonic mean, arithmetic mean, and geometric mean, and several others (https://en.m.wikipedia.org/wiki/Average)
Just out of interest, what kinds of functions would you expect to have to write yourself, if you're not happy about calculating the average of a list of numbers?
for n in list
sum += n
return sum / len(list)
Which will fail but will probably be caught in the code review. Then a cleverer developer might think to write l = len(list)
for n in list
sum += n / l
return sum
Which will also fail but in more subtle ways, hopefully the senior dev will catch it in the code review.Then they correct it to
l = len(list)
for n in list
sum += n / l
rem += n % l
sum += rem / l
rem = rem % l
return sum
But this could further optimized and might still be dangerous. This one might not be fixed at all in the code review.The best algorithm might be Knuth's which is fairly non-trivial and might not even be the fastest/most efficient given certain unique JS optimizations.
Do you want to do this for every 'trivial' function or would you rather import average where (in theory) someone knowledgeable has already done this work and benchmarks for you and you get the best performing algorithm basically for free?
I wonder if there will be a 3rd release with an updated averaging function.
Edit: to be fair, it was an optimization from reduce to for loop [https://github.com/bytespider/average/commit/7d1e2baa8b8304d...]
Pretty much every package management system gets cruft in it like this. Example: for a long time someone had uploaded a random Wordpress core install into Bower.
ducks
(Edit: typo)