Counterintuitive native array methods behavior (.length > 2^32)
# Ivan Vyshnevskyi (9 years ago)
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: www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.foreach
[1]: step 3 of ArrayCreate www.ecma-international.org/ecma-262/6.0/#sec-arraycreate
[2]: for example step 7 of Array.prototype.push www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.push
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,