View Full Version : Array max length?

DHTML Kitchen
01-01-2006, 02:59 AM
What is the longest possible array?

new Array(Number.MAX_VALUE) // ERROR.

01-01-2006, 04:25 AM
What is the longest possible array?

new Array(Number.MAX_VALUE) // ERROR.

I did a little searching but could not find a reference that stated what the max size number of elements could be. So I did a little testing on my own. I used a web page from www.w3schools.com (http://www.w3schools.com)

<script type="text/javascript">
var arr = new Array(3)
arr[0] = "John"
arr[1] = "Andy"
arr[2] = "Wendy"
document.write("Original length: " + arr.length)
document.write("<br />")
document.write("New length: " + arr.length)

If I use a number any higher than the one in bright blue in the above code (4,294,967,295) I would get the following error code in FoxFire:

Error: invalid array length
Source File: http://www.w3schools.com/jsref/tryit_view.asp (http://www.w3schools.com/jsref/tryit_view.asp)
Line: 14

I tried it on 3 different computers with different amounts of RAM, with the same results.

But I don't think it would be a good ideal to use an array that size.

01-01-2006, 10:19 PM
Even if each entry in an array that big were only one byte in size it would take w-------a--------y too long for the web page to download to be usable. If each entry were a lot larger it could easily exceed the entire space on your web hosting.

The practical limit is probably in the hundreds if not lower.

01-01-2006, 10:22 PM
JS doesnt put a limit on the size of an array. The only restraint is that of the user's computer. No matter the amount of memory/processor speed that a computer has it will still have a set limit, and for most the # will be close to that which you found.

01-03-2006, 05:51 PM

I think I found the reason for the limit of 4,294,967,295 elements in a javascript array. That is the max size of an interger (-2,147,483,648 to 2,147,483,648)

01-03-2006, 06:49 PM
Javascript currently only has one number type, floating-point, which is encapsulated as the object type "Number". Evidently, the actual value is held in binary as an IEEE Standard 754 Double. The range therefore slightly exceeds &#177;1.7&#215;10308. Integers can only be held accurately up to, I think, 53 bits plus sign, and numbers are only, at best, accurate to 15-16 significant decimal digits. Note, however, that any integer from 0 up to 253 = 9,007,199,254,740,992 in magnitude should be stored exactly, but relatively few decimal fractions can be. Only the numbers obtainable from one of those integers by multiplying by 2N-53 for any N in -1023 to +1023 can be held exactly. Check in detail, though.

That should explain it to you. Basically, unlike other programming languages JS only has one type of number. Other languages usually have more than one (integer,double,etc.)