On Wed, May 23, 2018 at 5:56 PM, Alexander Lichter <e...@lichter.io> wrote:
> An optimization would be great because in comparison to the existing
concat
> method, rest/spread is significantly slower at the moment (see
> https://jsperf.com/single-array-composition)

There's a limit to how much optimization can address the fact that
`[...original, ...additions]` has to create a new array. And creating the
array is significant, as we can see in the difference between `concat` and
`push`: https://jsperf.com/concat-vs-push-number-42

I've often wanted an `Array.prototype.append` (often enough I've been known
to add it on occasion; obviously only in app/page code, not in lib code). It
would be a good addition in my view. I tend to suspect the name isn't
web-safe, though. So that would need investigation and design.

The polyfill is trivial:

```js
Object.defineProperty(Array.prototype, "append", {
    value: function(...sources) {
        for (let o = 0, olen = sources.length; o < olen; ++o) {
            const source = sources[o];
            for (let i = 0, ilen = source.length; i < ilen; ++i) {
                this.push(source[i]);
            }
        }
        return this;
    },
    writable: true,
    configurable: true
});
```

I've avoided iterators and such as a first pass at optimization (and it
seems to do okay: https://jsperf.com/push-vs-append-number-42/1). Didn't
use `push.apply` because of the limits it has on it in some
implementations. This version assumes all entries are array-like and should
have their entries added to the array on which it's called. There are a lot
of other designs one could make, though. Just one argument (you can always
chain); a flattening version; etc., etc.

-- T.J. Crowder
_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to