Math.minmax

kai zhu kaizhu256 at gmail.com
Mon Oct 2 16:01:41 UTC 2017


hi xavier, i've attached some screenshots to explain why Math.minmax has little performance impact on a webapp as a whole.

here's a screenshot showing it takes 1) 30ms to run a single Math.min() operation one million times and 2) 50ms to run both Math.min() and Math.max() one million times.  assuming the hypothetical Math.minmax() ideally runs as fast as Math.min(), it will save you 20ms for every one million operations.

```javascript
/*jslint node: true*/
/*globals Float64Array*/
'use strict';

var ii, list1, list2, result, time;

list1 = new Float64Array(1000000);
list2 = new Float64Array(1000000);
for (ii = 0; ii < 1000000; ii += 1) {
    list1[ii] = Math.random();
    list2[ii] = Math.random();
}
// pre-trial run to get vm-optimization shenanigans out of the way
for (ii = 0; ii < 1000000; ii += 1) {
    result = Math.min(list1[ii], list2[ii]);
    result = Math.max(list1[ii], list2[ii]);
}
time = Date.now();
for (ii = 0; ii < 1000000; ii += 1) {
    result = Math.min(list1[ii], list2[ii]);
}
console.log((Date.now() - time) + 'ms - 1,000,000 min calculations');
time = Date.now();
for (ii = 0; ii < 1000000; ii += 1) {
    result = Math.min(list1[ii], list2[ii]);
    result = Math.max(list1[ii], list2[ii]);
}
console.log((Date.now() - time) + 'ms - 1,000,000 min and max calculations');
```

here's a screenshot showing how long it takes to load a google-search webpage (arguably the fastest-loading real-use webpage in the world), under ideal conditions with pre-caching and all.  as you can see, it takes on average ~700ms to load the entire page with caching.


think about it.  the performance savings of 20ms for one million Math.minmax operations is at best gonna improve your webapp performance by less than 3% (assuming a 700ms load-time is all the webapp does).

> On Oct 2, 2017, at 8:10 PM, Xavier Stouder <xavier at stouder.io> wrote:
> 
> As adviced by annevk on IRC, I'm gonna explains the results better.
> 
> Minmax is more efficient that a min and a max : you loop one time
> instance of two. Seems logic.
> 
> To not destructurate the array of arguments is of course better because
> at the end, by destructurating you're gonna work on an arguments array.
> 
> I actually made the proposal this way to fit with existing min and
> max (taking arguments separately). Gonna edit the proposal to accept an
> array directly.
> 
> Don't know what Boris mean when he talks about recreation bits
> 
> Using both min and max is a regular use case of these functions, so x2
> improvement on this is an existing need. I mentioned an example of use
> above.
> _______________________________________________
> 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/20171003/21c4e05f/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Screen Shot 2017-10-02 at 11.41.20 PM.png
Type: image/png
Size: 335339 bytes
Desc: not available
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20171003/21c4e05f/attachment-0002.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Screen Shot 2017-10-02 at 11.29.26 PM.png
Type: image/png
Size: 689278 bytes
Desc: not available
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20171003/21c4e05f/attachment-0003.png>


More information about the es-discuss mailing list