JavaScript array random index insertion and deletion

Posted by Tomi on Stack Overflow See other posts from Stack Overflow or by Tomi
Published on 2010-12-30T23:14:57Z Indexed on 2010/12/30 23:54 UTC
Read the original article Hit count: 171

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about arrays