Math.minmax

J Decker d3ck0r at gmail.com
Mon Oct 2 15:58:00 UTC 2017


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

> No problem Boris, I edited this times a long time ago.
>
> Naveen, you missed he point. In fact, I just added your code the
> benchmark (link aboce) and it has catastrophic performances.
>

Ya, that's a lot of array creations, not to mention the callback in the
reduce()...

```js
const minMax = (arr=[]) => { let result=[Infinity, -Infinity]; for( let
i=0;i < arr.length;i++ ) {
  result[0] = Math.min(arr[i], result[0]);
  result[1] = Math.max(arr[i], result[1]);
 }
 return result;
}
```
although I suspect this will be faster...
```js
const minMax = (arr=[]) => { let result=[Infinity, -Infinity]; for( let
i=0;i < arr.length;i++ ) {
  result[0] = arr[i] < result[0] ? arr[i]:result[0];
  result[1] = arr[i] > result[1]? arr[i]: result[1];
 }
 return result;
}
```



> _______________________________________________
> 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/00701d42/attachment.html>


More information about the es-discuss mailing list