JavaScript array random index insertion and deletion
- by Tomi
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.