3

I am relatively unfamiliar with JavaScript, and I was recently told that a JavaScript array contains a length variable of type Number. This length is automatically updated as the array is updated to the number of elements in the array.

However, I was also told that internally, JavaScript uses a 64-bit floating point representation for its Number class. We know that floating point arithmetic cannot exactly represent all integers within its range.

So my question is, what happens with large arrays, where length + 1 cannot exactly represent the next largest integer in the sequence?

Thomas Russell
  • 5,870
  • 4
  • 33
  • 68
  • According to [this](http://stackoverflow.com/questions/6154989/maximum-size-of-an-array-in-javascript) the maximum length of an Array is `4,294,967,295`. `Number.MAX_SAFE_INTEGER` is `9,007,199,254,740,991` so you won't have to worry because the engine won't let you get that far – CodingIntrigue Jul 03 '15 at 13:35
  • @RGraham Thank you, if you write that as an answer I will accept it. What happens when you reach that length? Does the interpreter just complain at you and not let you update the array? – Thomas Russell Jul 03 '15 at 13:35

2 Answers2

5

According to this the maximum length of an Array is 4,294,967,295. Number.MAX_SAFE_INTEGER is 9,007,199,254,740,991 so you won't have to worry because the engine won't let you get that far, example:

new Array(4294967296); // RangeError: Invalid array length

Relevant part of the spec:

  1. c. Let newLen be ToUint32(Desc.[[Value]]).
    b. If newLen is not equal to ToNumber( Desc.[[Value]]), throw a RangeError exception

So given our example length 4294967296:

var length = 4294967296;
var int32length = length >>> 0; // Convert to int32
int32length === 0; // Can't represent this as int32
length !== int32length; // Therefore RangeException
Community
  • 1
  • 1
CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176
  • 2
    @Shaktal You're welcome. This seems like it's right, but someone with more knowledge of JS internals may prove me wrong! – CodingIntrigue Jul 03 '15 at 13:37
  • 2
    It seems to make logical sense, I will wait for a while before accepting to make sure though! – Thomas Russell Jul 03 '15 at 13:38
  • It's worth remembering that an array under the hood is just like any other JavaScript object and you can trick it like so. var tmp = []; tmp[0] = "0"; tmp[4294967294] = "4294967294"; console.log(tmp); Array [ "0", <9 empty slots>, 4294967285 more… ] which means you can 'fake' the size by only populating two elements of the array, the first, and the last. – NMunro Jul 03 '15 at 13:51
  • @NeilMunro But `var temp = [0]; temp[4294967296]; temp.length === 1;`, so you can't get a true length like that. All you're doing there is putting a string key onto an object posing as an array – CodingIntrigue Jul 03 '15 at 14:02
  • It's not an object posing as an array, an array is just syntactic sugar around a regular object: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf Page 95. "An array is an exotic object that gives special treatment to array index property keys". It's just a little caveat of the... Special way JavaScript treats arrays that under certain circumstances might be useful to know. – NMunro Jul 03 '15 at 14:09
  • I think we're both saying the same thing :) i.e. An array is an object posing as what is traditionally thought of as an "array" in other languages – CodingIntrigue Jul 03 '15 at 14:11
0

The maximum length of an array according to the ECMA-262 5th Edition specification is bound by an unsigned 32-bit integer due to the ToUint32 abstract operation, so the longest possible array could have 232-1 = 4,294,967,295 = 4.29 billion elements. This is according to Maximum size of an Array in Javascript..

so I guess @RGraham is right

Community
  • 1
  • 1
KAD
  • 10,972
  • 4
  • 31
  • 73