Array.prototype.remove(item)
T.J. Crowder
tj.crowder at farsightsoftware.com
Fri Nov 10 12:09:52 UTC 2017
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20171110/d2ae2553/attachment.html>
More information about the es-discuss
mailing list