I’m inserting some items into array with randomly created indexes, for example like this:
var myArray = new Array();
myArray[123] = "foo";
myArray[456] = "bar";
myArray[789] = "baz";
...
In other words array indexes do not start with zero and there will be “numeric gaps” between them. My questions are:
- Will these numeric gaps be somehow allocated (and therefore take some memory) even when they do not have assigned values?
- When I delete myArray[456] from upper example, would items below this item be relocated?
EDIT:
Regarding my question/concern about relocation of items after insertion/deletion – I want to know what happens with the memory and not indexes. More information from wikipedia article:
Linked lists have several advantages
over dynamic arrays. Insertion of an
element at a specific point of a list
is a constant-time operation, whereas
insertion in a dynamic array at random
locations will require moving half of
the elements on average, and all the
elements in the worst case. While one
can “delete” an element from an array
in constant time by somehow marking
its slot as “vacant”, this causes
fragmentation that impedes the
performance of iteration.
No. JavaScript arrays aren’t really arrays at all (see below), and the unused indexes consume no memory.
If you’re talking about array indexes, it depends on how you delete it: If you use the
deletekeyword, no. If you use thesplicefunction or similar, yes. In terms of memory, no, other entries are not relocated (regardless), and any memory that was referenced by an entry that no longer exists (whether because of adeleteor aspliceor apopor similar) becomes available to be reclaimed by the garbage collector. Linked lists have virtually no advantage in JavaScript over arrays or plain old objects, and you rarely see them. Adding to a JavaScript array (or object) is likely to be a near-constant-time operation (implementations will probably need to do hashing and possibly some traversal of B-tree structures or similar, but that’s totally implementation-specific), as is deletion.For what you’re describing, as Zevon pointed out, you may not want an array at all. You really only need an array if you need a
lengthproperty or one of the array functions that relies on it. Otherwise, you’re better off with a plain old object:That’s perfectly valid JavaScript. The values you’re using in brackets (123, etc.) are coerced to strings (whether you’re dealing with an array or a plain object), and so the key is really “123”, etc. (whether you’re using an
Arrayor anObject). You can even loop through them, with thefor..incontrol structure (details here).What do I mean by “…aren’t really arrays at all”? Literally that. JavaScript objects are key->value maps, and JavaScript arrays are nothing more than objects that have keys and values and special handling for keys that are numeric strings, and a special
lengthproperty. Although we conventionally write array “indexes” as numbers, like all property names they are strings —a[0]is converted toa["0"](although implementations are free to optimize this as long as the behavior remains as per the spec). This is covered by Section 15.4 of the specification, which starts with this paragraph: