1

I am new to JavaScript and lately been coding in PHP which I am looking to port to JavaScript. PHP has a Map implemented directly into its Array container class, which does not exists in the default language of JavaScript.

Everyone I seem to read says to use an Object for an associative array, but after reading this, specifically:

Property Lookup

When accessing the properties of an object, JavaScript will traverse the prototype chain upwards until it finds a property with the requested name.

When it reaches the top of the chain - namely Object.prototype - and still hasn't found the specified property, it will return the value undefined instead.

It would seem Object is not an efficient solution to associative arrays, especially when your desire array may contain 10's of 1000's.

What is an efficient alternative to map/associative array within JavaScript? Is there a good 3rd party library that offers a great container class implemented as map/assoc. array? I need to be able to easily and efficiently create large multi-degree associate arrays for various indexing strategies within my code, thus I need optimal sort and search algorithms.

Forgive me if all this seems obvious, but everything keeps pointing me to implement my assoc. array as an Object and I believe that is not the most optimal approach. Any help and guidance is greatly appreciated.

slhck
  • 36,575
  • 28
  • 148
  • 201
j_p
  • 29
  • 5
  • This really depends on the browser. Modern browsers are optimised. IE6 is slow as hell – Raynos Jun 11 '11 at 22:39
  • I should have clarified that this is for server-side JavaScript within the Node.js framework (using v8 JavaScript Engine). Which results in a lot of code logic that would normally not be done within the browser, rather as a network service. – j_p Jun 11 '11 at 22:58
  • in that case don't underestimate V8. Never underestimate V8. Use an object, array or linked list depending on how your using the data type (linked lists are great for cheap removal). – Raynos Jun 11 '11 at 23:00
  • I should also note that I need multi-dimensional functionality is critical, sometimes up to 4 keys wide. In some cases I search of the first one or two keys and other I keep to search/match all keys (obviously always for insert). – j_p Jun 11 '11 at 23:29

2 Answers2

1

As Object is the top-most prototype of the chain (you are not using sub-objects with sub-prototypes) there is no chain to evaluate. Thus Object.prototype is the only prototype it will access for checking for properties. Thus, there is not really a problem like you implied with the quote.

Kissaki
  • 8,810
  • 5
  • 40
  • 42
  • So implementing an Object containing 100,000 properties (key/value) in order to have assoc. array behavior is optimal approach in JavaScript? When I search for a specific "key" will it use an optimal binary search (or other optimized algorithm)? Will these keys always be sorted? From what I read from link about properties are located by a sequential search for requested property. – j_p Jun 11 '11 at 22:02
  • @j_p its not an associative array. Objects don't have order. It's just an un-ordered hash table. It's not binary search, it's O(1) look up time. Either use the hash table (object), an Array (array) or a linked list (implement one) – Raynos Jun 11 '11 at 22:26
  • I didn’t say it was the best way, just that it’s not as bad of a way as you implied (or I think your thought). I’m not sure of the implementation details, but it’s the simplest way to do it. Raynos already gave some details about the "internals". – Kissaki Jun 11 '11 at 22:42
1

The quote in your question really has nothing to do with how optimized a property look up is. When you do this: var x = {} and then x.foo, it will check if foo exists. If it doesn't, then it won't "go up the chain" and look elsewhere because x is already the most primitive type of object.

What you are really asking about is how optimized the string lookup is, given that you've got an object. That said, check this out:

http://www.timdown.co.uk/jshashtable/

It's an implementation of a hash table in JS. I didn't check out how optimized it was, but in Chrome it was 5x to 10x slower than just using regular JS objects as a hash table. (Test size of 10000 elements.) I wrote a simple JS hash table myself and got the same sort of results.

I would imagine, but obviously you should test this out, that all browsers implement string keys as an optimized hash internally, much like how the PHP array would work. It would seem quite silly if it didn't, from a performance standpoint.

Matthew
  • 47,584
  • 11
  • 86
  • 98
  • Why would you want a hash table :( you have objects. If you made a linked list then fair enough – Raynos Jun 11 '11 at 22:26
  • @Raynos, there are some benefits to the JS based hash table. The author explains it in the provided link, but speed is not one of them. The OP was simply worried that the native object property lookup was not optimized. – Matthew Jun 11 '11 at 22:29
  • Thanks for the link, I will check it out :) I am porting code to be used as server side JavaScript within the Node.js framework (Google JavaScript v8 engine), so performance and optimal sort and key lookup will be most important. My entire codebase is dependent on multi-d maps for internal lists and data caching. If server side JavaScript does not show promise I will be forced to code in C :p – j_p Jun 11 '11 at 22:54
  • Since this is server side, that will make your benchmarks easier. My guess is that V8's performance would be no worse than something like PHP when dealing with the object hash table. – Matthew Jun 11 '11 at 23:14
  • jshashtable is designed to be flexible, which comes at the price of some performance. It could be optimized for particular cases (such as restricting keys to just strings), but doing so would bloat the code. In any case, the best it can hope for in its current form is to match the performance of native JavaScript object property lookup. – Tim Down Jun 12 '11 at 00:24