advertisement -- please support sponsors

the maximum array size

I want to define a large array within a JavaScript-enabled web page. What is the maximum size of array I can define? Is there a limit in the language?

Although various, platform-dependent implementations of JavaScript surely impose "theoretical" limits on the maximum number an elements an array can have, such limits are not likely to be less than the maximum capacity of the user's machine. In other words, the machine will run out of memory long before the array runs out of slots for elements.

sparsely populated arrays

JavaScript handles arrays quite differently from other languages. The reason that many languages require you to declare the size of an array when you create it is because memory for the entire array must be allocated at the time of the array's creation. JavaScript, however, allocates memory for arrays only when the memory is actually needed. Thus, the expression to create a new array, new Array (), does not require any parameters.

JavaScript arrays are sparsely populated. This means that if things is an array, and you assign values to both things [23] and things [200], space for elements 23 and 200 will surely be allocated, but space for elements 24 through 199 may not be. Even so, the expression "things . length" will return 201, because the last defined element in the array is element #200. This does not mean, however, that storage for 201 elements has actually been allocated.

non-standard array indeces

Did you know that array indeces can be values other than positive integers? In fact, when it comes to indexing arrays, you can use just about anything! For example, the expressions "things [document . window]" and "things ["giraffe"]" are perfectly valid in JavaScript. Whatever is put between the square brackets will serve as a key for looking up the element in the array (actually, it's probably a hash table, but that's beside the point). Anything written to an array can be read from the array, as long as the same or equivalent key is used.

There are even more surprises about arrays. Did you know that all JavaScript objects are just arrays? Try looking up the value of window ["location"], and you will see that it is exactly the same as window . location! Here's an even weirder one: Try the statement, window ["alert"] ("Wow!") and you will see that this is equivalent to window . alert ("Wow!")!

Pretty trippy, isn't it?

Now, as I inferred earlier, the expression "array_name . length" does not return the number of elements stored in the array. Instead, it returns one of the following two values, whichever is greater:

  1. The value of the highest positive integer that was used as an index for storage in the array, plus 1.
  2. The declared initial size of the array, as given in the new statement that created it. This is 0 if the constructor was called without parameters (i.e., new Array ()).

These rules have exceptions, because there are certain positive integers which are not considered normal array indeces. Consider the following program:

a = new Array ();
a [0x3ffffffe] = 0;
document . write ("<p>a . length = " + a . length + "</p>");

b = new Array ();
b [0x3fffffff] = 0;
document . write ("<p>b . length = " + b . length + "</p>");

c = new Array ();
c [0x40000000] = 0;
document . write ("<p>c . length = " + c . length + "</p>");

d = new Array ();
d [0x40000001] = 0;
document . write ("<p>d . length = " + d . length + "</p>");

On my system (Windows 95, Netscape 4), this produces the following output (the results may be different on your system):

a . length = 1073741823

b . length = undefined

c . length = 0

d . length = 0

Note how the expression array_name . length freaks out when indeces higher than 0x3ffffffe (1073741822) are used. I guess this means that if you wish to restrict your array indeces to normal, positive integers, then the largest integer you can use is 0x3ffffffe (1073741822).

However, if you don't care about the array's length property working correctly, then any index is valid -- strings, objects, negative numbers, the works -- and the capacity of the array is limited only by the capacity of the user's machine.

Charlton Rose
30 August 1997