Why does array.concat copy the array rather than doing for(e...) array.push(e);

I’ve just realized that array.concat creates a copy of the array and returns it. This requires an additional assignment, something I was never fond of.

Isn’t this less efficient than the following code which can be hosted in an Array static extension?

for (newElement in elements) array.push(newElement);

I’m probably missing something because if the above would have been more efficient, I’m pretty sure it would have been in Std since ages ago :smiley:.

In other words, why does array.concat create another copy?

That’s how it’s defined in the ECMAScript standard for ages, so both flash and js implemented it that way. And FWIW even PHP does it that way: http://php.net/manual/en/function.array-merge.php

So it’s not really a design choice. That said, you can just make a static extension:

class ArrayTools {
  static public function append<T>(target:Array<T>, toBeAdded:Array<T>) {
    for (x in toBeAdded) target.push(x);
    return target;
  }
}

Then add using ArrayTools; to a toplevel import.hx and you’re all set :wink:

4 Likes

Already did that my sibling :slight_smile:. Well, of course, not with all your bells and whistles of import.hx, mister haxor (yeah, knew about that too, I guess I’m graduating Haxe Noobs hopefully?)

1 Like

The reason why concat and many other array operations will return a copy is that methods that modifies the array tends to be an excellent footgun.
I would be more concerned with avoiding side effects than any marginal performance cost.

Since the need for actually doing an append instead is quite rare, having a function for it would encourage bad practices, and I would prefer to be explicit to make it obvious what is happening by simply doing b.forEach(val -> a.push(val)).

4 Likes

Yeah, and I just realized that this would seriously annoy people that just want to do a for (element in originalArray.concat(some extra elements)).

“Footgun.”:smiley: … I’m gonna have to remember that one.

I think that everyone rightly assumes that library-calls do not have side-effects: they do not alter the parameters sent to them. If you want to modify the array, then, as Leo says, write a one-liner to do it, right then and there. With a comment to make it even more obvious.

1 Like