Wanted: standard Array function to append an array's

Jeff Walden jwalden+es at MIT.EDU
Fri Jul 29 14:19:52 PDT 2011


On 07/29/2011 05:01 AM, Andrea Giammarchi wrote:
> I may be late here, but what's wrong with
>
> firstArray = firstArray.concat(secondArray); ?
>
> If there are still problems I would say no "magic method" can solve them, isn't it?

That creates a new array rather than mutate the array originally referred to by |firstArray| here, and I originally specified that only mutation was acceptable, because creating a new array requires extra space proportional to the length of |firstArray|.

But I assume you're arguing that an engine could recognize that the copy could be transformed into a mutation.  You'd have to prove that was the *only* reference to the original array in order to perform that optimization, or prove that it was observably correct to do that.   Such analysis is tricky and costly time-wise.  Relying on it would also be performance-fragile.  If you were pushing incrementally onto an array (say, because you were appending arrays of bytes read from a network stream, saving them up to be processed all at once), the entire process is O(n) in bytes processed with push-through-mutation.  But it's O(n**2) with push-by-copying.  Thus you'd have to require the developer to understand when the optimization could be applied, in order to structure his code such that it would be applied.  That level of understanding of compilers, and of the algorithms actually used to implement them (which won't be publicly available for some engines), seems way way beyond wh
at can reasonably be expected of web developers.

Jeff


More information about the es-discuss mailing list