Javascript Array lookup efficiency: associative vs. stored associative?

I've been reading, and they're saying that associative arrays won't give you the same efficiency as arrays. An associative array can look things up in O(N) time, where an array can look things up in O(1).

Here's my question: which one would be more efficient in terms of looking up values quickly and not hogging too much memory?

Associative:

var myVars=new Array(); 
myVars['test1'] = a;
myVars['test2'] = b;
myVars['test3'] = c;
... (up to 200+ values)

echo myVars['test2'];

Stored Associative:

var myVars=new Array(); 
var TEST1 = 1;
var TEST2 = 2;
var TEST3 = 3;
... (up to 200+ values)

myVars[TEST1] = a;
myVars[TEST2] = b;
myVars[TEST3] = c;
... (up to 200+ values)

echo myVars[TEST2];
5
задан M. Tibbits 10 August 2011 в 06:08
поделиться