1

I have a very large hard coded index array that I would like to easily convert to an associative array so lookups are much quicker.

var arr = ["a", "b", "c"];

right now I am looping through arr and comparing it's values to a value to see if there is a match. Now that I have hundreds of elements it's getting rather slow and it would be faster to have an associative array.

It seems I can't just do

var arr = {"a", "b", "c"}; 

I can't really add a value since it is too slow.

Sure I could copy the elements to an associate array or sort the array and do a binary search but it would be much easier to just able to assign a default value to the array elements and use the syntax above.

I guess this is not possible though?

7
  • what does adding a value to an object property have to do with speed? Commented Feb 17, 2013 at 5:03
  • @thesystem If it's using a hash lookup, it can hash the key to find the bucket it's in, which should reduce the lookup space. Commented Feb 17, 2013 at 5:04
  • @alex: no, I was referring to this: "I can't really add a value since it is too slow." Commented Feb 17, 2013 at 5:06
  • How are you creating your array? Commented Feb 17, 2013 at 5:07
  • I said they are hard coded, manually, and already exist, I'd have to manually add all values by hand(I could use a search and replace but still slow). I'd have to add the values by hand to all the keys I've added. I am not using the values. Just the keys for O(1) lookup. Commented Feb 17, 2013 at 5:09

4 Answers 4

1
var mapLookup = arr.reduce(function (accumalator, value) {
    accumalator[value] = true;
    return accumalator;
}, {});
Sign up to request clarification or add additional context in comments.

1 Comment

IE 8 doesn't support reduce, and it's still fairly prevalent
1

Why not: var arr = {"a":1, "b":1, "c":1};

1 Comment

because I'd have to add :1 to about 1000 + elements and to every element I manually added to the array.
1

Converting arrays to associative arrays seems to be the easiest and very fast:

var arr = ["a", "b", "c"];
var arrA = {}; for(var i = 0; i < arr.length; i++) arrA[arr[i]] = 0;

then just use key in arrA for O(1) lookup. (it would be easier to have the ability not have to explicitly supply a value to a key but....)

Essentially

if (key in arrA) ...

replaces

for(var i = 0; i < arr.length; i++) if (key == arr[i]) ...

which essentially is O(n) vs O(n^2) when uses inside a loop.

1 Comment

If you really want to shorten it, you could do eval("({" + arr.join(":1,") + ":1})")
1

Most modern browsers support Array.indexOf(). This will return -1 if your search bears no results.

Are you actually having speed issues, or are you just pre-optimizing? The container you should be using is an array. You have an array of elements - they don't associate with any other values, so why put them in a map container?

Also, it sounds like you want a set, where you have a unique set of elements.

2 Comments

OP wants to compare against the value, not the a,b,c keys.
Yes, I have speed issues, I've fixed them by converting my arrays to associative arrays and it's about 1000 times faster now. (instant)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.