When is an array not an array?
- special handling for keys that are numeric strings
- a special length property
- and some functions they inherit from Array.prototype
All of this is covered by Section 15.4 of the specification, which starts with this paragraph:
Array objects give special treatment to a certain class of property names. A property name P (in the form of a String value) is an array index if and only if ToString(ToUint32(P)) is equal to P and ToUint32(P) is not equal to 2^32−1. A property whose property name is an array index is also called an element. Every Array object has a length property whose value is always a nonnegative integer less than 2^32. The value of the length property is numerically greater than the name of every property whose name is an array index; whenever a property of an Array object is created or changed, other properties are adjusted as necessary to maintain this invariant. Specifically, whenever a property is added whose name is an array index, the length property is changed, if necessary, to be one more than the numeric value of that array index; and whenever the length property is changed, every property whose name is an array index whose value is not smaller than the new length is automatically deleted. This constraint applies only to own properties of an Array object and is unaffected by length or array index properties that may be inherited from its prototypes.
There are some consequences to this:
- Barring implementation optimizations, accessing array elements by index is not a constant-time operation as it is in (say) C; like all maps, it will vary depending on the layout of the key storage, the size of the map, and the specific key you're looking up.
- Barring implementation optimizations, using an array is no more efficient than using a plain object.
- Arrays can have non-index properties, which can be handy. For instance, this is perfectly valid:
This is one reason why it's important to understand for..in loops before using them on arrays (see Myths and realities of for..in).
var a = ["zero", "one", "two", "three"];
a.foo = "bar";
alert(a.length); // alerts "4"
alert(a.foo); // alerts "bar"
None of which means that we shouldn't use arrays for ordered lists of things. We should. Doing so is useful semantically, and of course it's entirely possible that an implementation will optimize some of those things above. But it's important to remember what you're dealing with, and taking advantage of an array's non-array nature can be very useful sometimes.