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 .
In other words, why does array.concat create another copy?
Already did that my sibling . 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?)
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)).
“Footgun.” … … 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.