Counterintuitive native array methods behavior (.length > 2^32)

Ivan Vyshnevskyi sainaen at gmail.com
Mon Jul 13 12:59:43 UTC 2015


I believe, it's fixed in ECMAScript 2015: now check for `length` being <
2^32 is performed only on array creation [1], and < 2^53 when length is
going to be updated [2].

See, for example, current algorithm of Array.prototype.forEach:
http://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.foreach

[1]: step 3 of ArrayCreate
http://www.ecma-international.org/ecma-262/6.0/#sec-arraycreate
[2]: for example step 7 of Array.prototype.push
http://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.push

// Sorry for duplication, answered in the private email first.

2015-07-13 14:18 GMT+03:00 Michał Wadas <michalwadas at gmail.com>:

> Steps to reproduce:
>
> let p = {
> 0: 'a',
> 1: 'b',
> 2: 'c',
> length: Math.pow(2,32)+1
> };
> Array.prototype.forEach.call(p, (el)=>console.log(el)); // logs only 'a'
>
> The cause of this behaviour is obvious for anyone that reads
> specification - every native Array.prototype methods perform abstract
> operation `ToUint32(this.length).
>
> Anyway - this behaviour obviously counters intuition. I think it would
> be totally OK to allow native methods to throw when .length > 2^32,
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150713/3b44baf9/attachment.html>


More information about the es-discuss mailing list