[英]What is the fastest way to scan a JS array
I am making a local login using JavaScript and I need to know the fastest way to scan an array. 我正在使用JavaScript进行本地登录,我需要知道扫描数组的最快方法。
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 并且有两个HTML输入
<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. 我需要知道查看HTML输入中的值是否与任何用户名和密码匹配的最快方法。
I have tried a while function: 我试过一段时间的功能:
while(counter1 < usernames.length){
//testing goes here
counter ++;
}
And an "if / repeate" function 以及“if / repeate”功能
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?" 这不是“在JavaScript中循环数组的最快方法是什么?”这个问题的重复。 because I am open to more than a
因为我比一个人更开放
for
loop 环
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). 如果是这样,我肯定会使用
Array#indexOf
或Array#includes
卸载到本机代码(如果你需要知道的是是否匹配)。 Otherwise, really, it doesn't matter. 否则,真的,没关系。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.