Math.minmax

J Decker d3ck0r at gmail.com
Mon Oct 2 23:03:26 UTC 2017


```js
const minMax = (arr=[]) => { if( arr.length==1 ) return [arr[0],arr[0]];
   const result=[Infinity, -Infinity];
   for( let i=0;i < arr.length;i++ ) {
      result[0] = arr[i] < result[0] ? arr[i]: ((result[1] = arr[i] >
result[1]? arr[i]: result[1]), result[0]) ;
   }
   return result;
}

Would be slightly faster to only do the max if it's not a min. Which fails
if there's only 1 number to compare.

On Mon, Oct 2, 2017 at 9:12 AM, Xavier Stouder <xavier at stouder.io> wrote:

> JDecker: Just added your solution on the benchmark, it beats every
> others solution and it's a elegant solution.
>
> Kai Zhu: We can't see the screenshot. But please take in consideration
> that it's been a long time that ECMAScript isn't only used in webapp,
> and that some of applications using it can eat more than a million
> numbers.
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20171002/1b9d7ebe/attachment.html>


More information about the es-discuss mailing list