Javascript objects themselves are maps, so for example:
var userCache = {}; userCache['john'] = {ID: 234, name: 'john', ... }; userCache['mary'] = {ID: 567, name: 'mary', ... }; userCache['douglas'] = {ID: 42, name: 'douglas', ... }; // Usage: var id = 'john'; var user = userCache[id]; if (user) alert(user.ID); // alerts "234"
(I didn't understand if your "userid" would be "john" or 234, so I went with "john" above, but you could use 234 if you want.)
This is before implementing how keys are stored and whether the card is a hash card or some other structure, but I did it with hundreds of objects and was completely satisfied with the performance even in IE, which is one of the slower implementations (at the moment).
This works because there are two ways to get the properties of a Javascript object: through dotted notation and through parentheses with a notation. For instance:
var foo = {bar: 10}; alert(foo.bar); // alerts "10" alert(foo['bar']); // alerts "10" alert(foo['b' + 'a' + 'r']); // alerts "10" s = "bar"; alert(foo[b]); // alerts "10"
It may seem strange that this copied syntax for getting an object property by name is the same as getting an array element by index, but in fact the array indices are object properties in Javascript. Property names are always strings (theoretically), but automatic conversion happens when you do things like user[234] . (And implementations are free to optimize the transformation if they can, provided semantics are preserved.)
Edit Some bits and fragments:
Quoting via cache
And if you want to iterate over the cache (and based on your question about the next steps ), you might want others to read this question)
var key, user; for (key in userCache) { // `key` receives the name of each property in the cache, so "john", // "mary", "douglas"... user = userCache[key]; alert(user.ID); }
The keys are looped in a certain order, it depends on the browser.
Delete from cache
Suppose you want to completely remove a property from the cache:
delete userCache['john'];
Now the object no longer has the "john" property.