const biggestItem = arr => Math.max(...arr);
Returns the highest coerced numerical item of the array.
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
Top comments (9)
I'm curious how the performance compares to:
Especially in the case of large arrays, I'm not sure what the implications are (if any) to spreading a large array to function arguments
Try this - the first method seems to be about 10 times as fast
Interestingly, if you switch to a typed array, the results are reversed -
reduce
is 10 times as fast on Firefox, and nearly twice as fast on ChromeAnd unsurprisingly, this is even faster:
Thank you both for the performance analysis, it's very helpful to have these additional details :)
Probably need to be a bit careful describing what the function does. The function here will actually return the highest coerced numeric value from the array ('biggest' is also slightly ambiguous):
It will also return
-Infinity
if an empty array is passed, and returnNaN
if any item in the array cannot be coerced to aNumber
.Thank you for your comment.
I will update the article
Too boring~! This just is a simple mathematical API.
Nobody needs your boring and simple comment 🥱, if it's not helpful to you, just ignore that post.