1

I am making a local login using JavaScript and I need to know the fastest way to scan an array.

Say there are two arrays:

usernames = ["uname 1", "uname 2", "uname 3"];
passwords = ["pswd 1", "pswd 2", "pswd 3"];

And there are two HTML inputs

<input id="username">
<input id="password">

I need to know the fastest way to see if the value from the HTML inputs matches any username and password.

I have tried a while function:

while(counter1 < usernames.length){
   //testing goes here
   counter ++;
}

And an "if / repeate" function

if(counter1 < usernames.length){
    //testing goes here
    setTimeout(currentFunction, 1);
}

This is not a duplicate of the question "What's the fastest way to loop through an array in JavaScript?" because I am open to more than a

for

loop

DMVerfurth
  • 172
  • 1
  • 13
  • 1
    Possible duplicate of [What's the fastest way to loop through an array in JavaScript?](https://stackoverflow.com/questions/5349425/whats-the-fastest-way-to-loop-through-an-array-in-javascript) – Pop-A-Stash Jan 18 '18 at 18:12
  • 2
    Could you elaborate why it has to be "the fastest"? Would a difference between 10ms and 100ms matter for your application? – georg Jan 18 '18 at 18:14
  • 2
    @georg: More like 10ns and 100ns. :-) – T.J. Crowder Jan 18 '18 at 18:14
  • Yes, the difference between 10ms and 100ms does matter, I do not want people on my login to have to wait any longer than they should. – DMVerfurth Jan 18 '18 at 19:43
  • This is not a copy of "What's the fastest way to loop through an array in JavaScript?" because of the fact that I am open to more than just a for loop. – DMVerfurth Jan 19 '18 at 21:59

1 Answers1

3

For "the fastest" to matter, I assume there must be a lot of entries in the array — like, millions. If so, I'd definitely offload to native code by using Array#indexOf or Array#includes (if all you need to know is whether there's a match). Otherwise, really, it doesn't matter.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875