0

I know how to use them, but I wonder how they work under the hood, and how slow they are compared to regular arrays.

My guess is that this:

myArray['value'] = 10;
myArray['another_key'] = 11;

is translated to something like this:

myArray[0][0] = 'value';
myArray[0][1] = 10;
myArray[1][0] = 'another_key';
myArray[1][1] = 11;
  • Do they actually work like this?
  • What happens when I try to get a value from an associative array? How much time does it take to get a value from an associative array?
  • Should I use them if my processing time is limited (e.g. inside a game loop)?

[EDIT]

Looks like my guess was wrong, and that the keys of the array are actually properties of an object. So, to collect all the answers I got in one place:

  • Associative arrays are objects
  • Keys are property names of said objects
  • No search is done when you get a value of a specific key of the array because what you actually do is fetching the value of an object property which takes next to no time.
Loupax
  • 4,728
  • 6
  • 41
  • 68
  • Did you think to google this even slightly? – Marcin Feb 13 '12 at 11:05
  • 1
    There is no such thing as associative arrays in Javascript. When you do `myArray['value'] = 10` you are actually creating an object called `myArray` with the property `value`. It's equivalent to this `myArray = { value: 10 }` – punkrockbuddyholly Feb 13 '12 at 11:05
  • 2
    MrMisterMan: if you see Wikipedias definition of associative array (http://en.wikipedia.org/wiki/Associative_array), I would say that JS has them: "In computer science, an associative array, map, or dictionary is an abstract data type composed of a collection of (key,value) pairs, such that each possible key appears at most once in the collection." Arguably using this term may confuse people not to realise that all JS objects behave in this way. – simon Feb 13 '12 at 11:08
  • possible duplicate of [Does Javascript have associative arrays?](http://stackoverflow.com/questions/7674922/does-javascript-have-associative-arrays) – Marcin Feb 13 '12 at 11:10
  • Oh, I see! So when I try to, let's say, get the value of myObject['name'] js simply returns me a value of a property, which takes constant time instead of searching a 2D array (which takes linear time). This actually made me answer my own question, but please make a response so I can accept it :) – Loupax Feb 13 '12 at 11:13
  • @Marcin That was not my question. My question was "how do they work"... – Loupax Feb 13 '12 at 11:14
  • @Loupax: Your question was based on a misconception, which that question answers. – Marcin Feb 13 '12 at 11:20
  • 1
    @simon good point, well made sir. – punkrockbuddyholly Feb 13 '12 at 11:20

3 Answers3

5

It is totally different.

First, never use arrays as associative arrays. Arrays only work properly with numerical indicies. For example:

var a = [];
a['foo'] = 'bar';
console.log(a.length) // prints 0

Not even objects are really appropriate for associative arrays if you think that its elements should maintain a certain order (e.g. like in PHP). That is not the case. But objects are good for hash tables, which is sufficient in most cases.

Internally, arrays are only objects as well. So when you have an array:

var a = [];
a[0] = 42;

then 0 is actually converted to a string and added as property to the object (note the last line (__proto__: Object)):

enter image description here

You can access that property with a['0'] as well.

Of course it could be that these operations are optimized internally for arrays, but that depends on the engine.

So in the end, a[0] = 42; or o['value'] = 42; (with o = {}) produce the same results

enter image description here

and accessing them with either a[0], o['value'] or o.value is the same as well.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
2

An associative array is just an object. Setting items is just setting properties, so your code is the same as:

myArray.value = 10;
myArray.another_key = 11;

Looking up properties in an object is pretty fast, so you shouldn't normally have any performance problems with that.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0

Javascript does not have associative arrays. You can use objects instead. http://www.quirksmode.org/js/associative.html

dotancohen
  • 30,064
  • 36
  • 138
  • 197
  • It is incorrect to say that javascript does not have associative arrays - objects *are* associative arrays (and more). – Marcin Feb 13 '12 at 11:12