NEWBIE: find array element

J

JohnS

Hello:

I am trying to find the fastest way to find out whether a string is an
element of an array. I can traverse the array and compare it to every
element ... but is there a better (read: faster) way?...

Tia

John.
 
E

Evertjan.

JohnS wrote on 04 jul 2003 in comp.lang.javascript:
I am trying to find the fastest way to find out whether a string is an
element of an array. I can traverse the array and compare it to every
element ... but is there a better (read: faster) way?...

If your array is sorted, you can do a binary search in the sense that you
start comparing in the middle and then to the middle of the half the
cmparison points to.

A "not found" of an array of 1024 is reached in 10 comarisons instead of
your 1024 comparisons.

A search of 1048576 is reached in 20 steps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top