DEV Community

Cover image for 1 line of code: How to get the average of an array
martin krause
martin krause

Posted on

1 line of code: How to get the average of an array

const average = arr => arr.reduce((a, b) => a + b, 0) / arr.length;
Enter fullscreen mode Exit fullscreen mode

optimised code (Benchmark)

const average = arr => arr.reduce((a, b) => a + b) / arr.length
Enter fullscreen mode Exit fullscreen mode

Returns the average of the sum of all items in a numerical array.
Beware of JavaScripts Automatic Type Conversion if your Array contains something else than Numbers.


The repository & npm package

You can find the all the utility functions from this series at github.com/martinkr/onelinecode
The library is also published to npm as @onelinecode for your convenience.

The code and the npm package will be updated every time I publish a new article.


Follow me on Twitter: @martinkr and consider to buy me a coffee

Photo by zoo_monkey on Unsplash


Discussion (9)

Collapse
jonrandy profile image
Jon Randy • Edited on

The initial value of 0 is unnecessary. The function will be faster without it (but it will error if you pass an empty array - which is arguably desirable behaviour since getting a result of NaN with an empty array in the original function is also nonsensical).

const average = arr => arr.reduce((a, b) => a + b) / arr.length
Enter fullscreen mode Exit fullscreen mode
Collapse
martinkr profile image
martin krause Author

Hi Randy,

thank you for your contribution.
I was curious about your suggestion and created a performance test on hasty.dev. When I run the tests on Chrome and Safari the original version is faster for me.
Do you mind taking a look?

Cheers,

Martin

Collapse
jonrandy profile image
Jon Randy • Edited on

On Chrome - the two methods are practically neck and neck. On Firefox, the second one is consistently faster, but not by a huge amount. The second should logically be faster as it performs 1 less loop than the one with the initial 0.

Performance Link

Thread Thread
martinkr profile image
martin krause Author

Logically yes, but it's like 10% faster - also at the url you set up - with an initial 0 for me.

Thread Thread
jonrandy profile image
Jon Randy • Edited on

On the link above (all tests on my macbook) - Chrome has near identical speed for both functions (occasionally the first will be faster than the other, but also sometimes the second). On Firefox, the second function is always faster. So, on balance it seems that without initialising the 0 is faster overall (as logically expected). The JS engine in Chrome is clearly doing some smarter optimisation

Update: Just tried Chrome and Firefox on Ubuntu too - same results as for the macbook

Thread Thread
martinkr profile image
martin krause Author

Hmm ... let's say ... we'll just go with the logic and I'll update the article and the code. :D
Thank you for your time and investigations.

Collapse
jonrandy profile image
Jon Randy

The code editing on Hasty is really buggy too

Collapse
zerodragon profile image
Zero Dragon

you are missing a > to create the phat arrow to define the first function (just after arr)

const average = arr => arr.reduce((a, b) => a + b, 0) / arr.length;
Enter fullscreen mode Exit fullscreen mode
Collapse
martinkr profile image
martin krause Author

Thank you!
I adjusted the article.