An advantage to having this an internal primitive is you can use vector
instructions to check 4-8 values in parallel and then end with a final step
of finding the max/min value of the vector. (integers can just use bit
hacks, float max/min has hardware acceleration).

The arithmetic mean could be similarly optimized via floating point vector
instructions (divide by length, horizontal add), but the median and mode
both would have to be done in serial IIRC and their algorithms are
considerably more complex. (You also need more than O(1) space for both.) I
could see the mean in a standard library module, but median and mode are so
rarely useful in general, not even Lodash has them. (Really, not even Java
has them.)

-----

BTW, I take back the claim of low use case or need for mean. (It does have
language and library precedent, and it's non-obvious to get right for
iterators.)
On Wed, Nov 21, 2018 at 09:02 T.J. Crowder <tj.crow...@farsightsoftware.com>
wrote:

> On Wed, Nov 21, 2018 at 7:23 AM Isiah Meadows
> <isiahmead...@gmail.com> wrote:
> >
> > It doesn't work as well with larger collections.
>
> For example, fails at somewhere between an array of length 125,000 and
> 126,000 on V8 in Chrome v70 (as does the `apply` trick, so the proposed
> polyfill would have the same problem): http://jsfiddle.net/xoLe5mg7/
> Using spread (it's not rest) also means that it would have to go through an
> iterator, which if the iterable is known to be an array is a lot of extra
> overhead (but also makes it more generally useful, so, swings and
> roundabouts).
>
> @Gbadebo Bello - What makes `reduce` not suitable to your use cases?
> Example:
>
> ```js
> function findMin(array) {
>     return array.reduce((a, b) => a < b ? a : b);
> }
> ```
>
> or if you want the type check:
>
> ```js
> function findMin(array) {
>     return array.reduce((a, b) => {
>         if (typeof a !== "number" || typeof b !== "number") {
>             throw new Error("The array must contain only numbers");
>         }
>         return a < b ? a : b;
>     });
> }
> ```
>
> (both off-the-cuff, apologies if there are any minor errors; the overall
> point should be clear regardless.)
>
> If the callback becomes important from a performance perspective, I'd
> expect it to get aggressively optimized.
>
> -- T.J. Crowder
>
_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to