0

I'm a noob and wrote a whole program without knowing the easy way to find an element in an array...

my_array.indexOf("find this value");

Is indexOf a lot better than storing how many elements are in an array, and looping through the array until you find the element you want? I could have simplified my code alot.

I tried to make my lookups constant time by using multiple arrays, and storing the keys. It makes insertions/deletes slow because I have to update the keys though.

Should I have just used indexOf?

Thanks

5 Answers 5

5

The vast majority of the time you are much better off to use a native function that has been optimized over whatever solution you come up with. Aside from that, however, you said something about storing the amount of elements in the array. Not sure why you did that when arrays have a .length property.

Sign up to request clarification or add additional context in comments.

Comments

2

Javascript basically has two types of collections: Arrays and hashmaps. Both are a bit special. The hash map is just an object with named properties. The keys are strings that you use to access the values directly. Here's an example:

// create the hash map
var hashMap = {};
// add a value
var key = "John Dillinger";
hashMap[key] = "Criminal";
// retrieve the value
var stuff = hashMap[key];

Javascript arrays have a double functionality. They are of course arrays, but are also stacks. A stack follows the "last in - first out" rule. Here's an example of an array and a stack:

// Array example
var anArray = []; // or: var anArray = new Array();
anArray[0] = "some value";
alert(anArray[0]); // pops up "some value"
// Stack example
var stack = [];
stack.push("first");
stack.push("second");
alert(stack.pop()); // pop up "second"

Finally, for some problems a linked list could come in handy. For that you use an object. Something like this:

var linkedList = {value: "stuff"};
linkedList.next = {value: "other"};
linkedList.next.next = {value: "yet another value"};
// Traverse the list
var node = linkedList;
while(node) {
  alert(node.value)
  node = node.next;
}

Given the problem that you describe, I would use a hash map. Just remember to choose the correct collection type for any given problem.

1 Comment

It's misleading to say that JavaScript has "hash maps." Indeed JavaScript objects (what you are actually creating when you say var someName = {};--100% equivalent to var someName = new Object;, incidentally) behave in many ways like hash tables (maps) and, indeed, are often implemented as hash tables, one should call a horse a horse and an Object an Object.
1

You could use a hash table implementation in javascript to map values to array indices.

Comments

1

Native functions should be faster since it would be the runtime-engine precompiled code.

However, indexOf wasn't implemented until version 1.6, meaning it doesn't work in jscript/IE afaik.

But I would just prototype a workaround for it in that case. native functions is usually your best option.

In your case however, it seems that you want a hashmap, which in js is just a regular object as Helgi pointed out.

Comments

0

It's probable that the implementation of the indexOf method just loops over the array until it finds the requested value because in the general case that's about all you can do. Using it would clean up your code but is unlikely to make it faster. (There are faster ways of searching arrays but they carry certain restrictions and/or up-front costs.)

You should use the right data structure for the job. Arrays are for situations where order is important. If you find yourself searching through them a lot you should probably be using a hash instead. Hashes are unordered but lookups happen in constant time (no searching).

Comments

Your Answer

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