Array.prototype.remove(item)

Isiah Meadows isiahmeadows at gmail.com
Fri Nov 10 13:04:17 UTC 2017


Also, most generic Array methods are specialized to array-like
objects, not iterables. My proposed semantics go along with this.

-----

Isiah Meadows
me at isiahmeadows.com

Looking for web consulting? Or a new website?
Send me an email and we can get started.
www.isiahmeadows.com


On Fri, Nov 10, 2017 at 7:09 AM, T.J. Crowder
<tj.crowder at farsightsoftware.com> wrote:
> On Fri, Nov 10, 2017 at 11:41 AM, Bob Myers <rtm at gol.com> wrote:
>>
>> What's wrong with this?
>
> I had the impression he was trying to avoid callbacks, just using `===`. But
> other than a missing `const` on the `for-of`, it looks nice and efficient --
> except that [it doesn't seem like `for-of` on arrays with the default
> iterator is much optimized yet][1]. FWIW:
>
> ```js
> function removeFromArray(array, item) {
>     let changed = false;
>     let j, i, len, elt;
>
>     for (j = i = 0, len = array.length; i < len; ++i) {
>         elt = array[i];
>         if (elt === item) {
>             changed = true;
>         } else {
>             array[j++] = elt;
>         }
>     }
>
>     array.length = j;
>     return changed;
> }
> ```
>
> Clunkier but apparently we're optimizing for speed...
>
> -- T.J. Crowder
>
> [1]: https://jsperf.com/for-of-vs-for-with-const-binding/1


More information about the es-discuss mailing list