Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do arrays in JavaScript work ? (ie without a starting dimension size)

I was reading this post the other night about the inner workings of the Array, and learned a lot from the answers posted, especially from Jonathan Holland's one.

So the reason you give a size to an array beforehand is so that space will need to be reserved beforehand, so that elements in the array will be placed next each other in memory, and thus providing O(1) access time, because of the pointer + offset traversal.


But in JavaScript, you can initialize an array like such:

var anArray = []; //Initialize an empty array, without a dimension

So my question is, since in JavaScript you can initialize an array Without specifying a dimension before hand, how is memory allocated for an array to still provide a O(1) access time since the 'amount' of memory locations is not specified beforehand ?

like image 908
Andreas Grech Avatar asked Dec 29 '08 07:12

Andreas Grech


People also ask

Is array size fixed in JavaScript?

Array lengths are not fixed in JavaScript.

Which is the correct way to create an array in JavaScript?

Using an array literal is the easiest way to create a JavaScript Array. Syntax: const array_name = [item1, item2, ...]; It is a common practice to declare arrays with the const keyword.

What is an associative array in JavaScript?

What is an associative array? Associative arrays are basically objects in JavaScript where indexes are replaced by user-defined keys. They do not have a length property like a normal array and cannot be traversed using a normal for loop.

What is array in JavaScript?

In JavaScript, array is a single variable that is used to store different elements. It is often used when we want to store list of elements and access them by a single variable.


1 Answers

Hmm. You should distinguish between arrays and associative arrays.

arrays:

A=[0,1,4,9,16];

associative arrays:

B={a:'ha',b:27,c:30};

The former has a length, the latter does not. When I run this in a javascript shell, I get:

js>A=[0,1,4,9,16];
0,1,4,9,16
js>A instanceof Array
true
js>A.length
5
js>B={a:'ha',b:27,c:30};
[object Object]
js>B instanceof Array
false
js>B.length
js>

How arrays "work" in Javascript is implementation-dependent. (Firefox and Microsoft and Opera and Google Chrome would all use different methods) My guess is they (arrays, not associative arrays) use something like STL's std::vector. Your question:

how is memory allocated for an array to still provide a O(1) access time since the 'amount' of memory locations is not specified beforehand ?

is more along the lines of how std::vector (or similar resizable arrays) works. It reallocates to a larger array as necessary. Insertions at the end take amortized O(1) time, namely if you insert N elements where N is large, the total time takes N*O(1). Those individual inserts where it does have to resize the array may take longer, but on the average it takes O(1).

like image 136
Jason S Avatar answered Sep 21 '22 06:09

Jason S