Array.prototype.concat result length (ES5.1)

Andrew Oakley andrew at ado.is-a-geek.net
Thu Jul 14 05:21:17 PDT 2011


I'm not sure the Array.prototype.concat algorithm (§ 15.4.4.4) is quite
right.

As far as I can see the length property of the result (A) is not
explicitly set anywhere in the algorithm so we are relying on the
[[DefineOwnProperty]] method of A to set the length for us when we add
indexed properties to it.

If we pass "sparse" arrays to the function then I think the results
should look like this:

[,,,,].concat([]) => [] (length 0)
[,x,,].concat([]) => [,x] (length 2)

This is not what browsers do and is a significant change from ECMAScript
3 (which set the length of A at the end of the algorithm)

Is it reasonable to add another step at the end of the algorithm to make
it match browser behaviour better:

Call the [[DefineOwnProperty]] internal method of A with arguments
"length", Property Descriptor {[[Value]]: n}, and false.


The other complication I've spotted (I think it crops up elsewhere too)
is what to do if n becomes larger than the maximum array index.  My
reading of ECMAScript 3 says that we *should* throw a RangeError but
nobody seems to do that.

Opera and Firefox seem to store n as a 32bit unsigned number - the
length wraps and they start putting properties at the beginning of the
array again.  I'm not sure what Chrome is doing (I can't find my values
in the returned array), IE says "out of memory" and I got bored waiting
for Safari.

-- 
Andrew Oakley


More information about the es-discuss mailing list