gpt4 book ai didi

Javascript:switch-case 结构是检查大量正则表达式的最快方法吗?

转载 作者:行者123 更新时间:2023-12-03 06:21:10 25 4
gpt4 key购买 nike

为了解决(正则表达式)检测多个条目的问题,从可读性和功效(速度、内存使用)的 Angular 来看,switch/case 方法似乎比 if/else 方法更好。但当检查的案件数量增加时,情况仍然如此吗?

就速度和内存使用而言,超过 20 个案例(以及超过 10 个中断)的 switch/case 句子仍然是解决该问题的最有效方法吗?

示例代码:

    for (var i in pieces) { // each pieces[i] is an unknown text
switch (true) {
case /^\"?Accession\"?/.test(pieces[i]):
case /^\"?Protein IDs\"?/.test(pieces[i]):
numeration[0] = i; // an array to store the "column" location
break;
case /^\"?Description\"?/.test(pieces[i]):
case /^\"?Protein names\"?/.test(pieces[i]):
numeration[1] = i;
break;
//etcetcetc...
}
}

最佳答案

要事第一

首先,尽可能将案例组合在一起会更好。

case /^\"?(Protein IDs|Accession)\"?/.test(pieces[i]):

这既提高了性能又减少了内存使用。

Switch 与 If/else

The switch statement is often cited in other programming languages as the hands-down better option for evaluating multiple conditions. This isn’t because of the nature of the switch statement, but rather because of how compilers are able to optimize switch statements for faster evaluation. Since most JavaScript engines don’t have such optimizations, performance of the switch statement is mixed.

Firefox handles switch statements very well, with each condition’s evaluation executing in roughly the same amount of time regardless of the order in which they are defined. That means the case of value equal to 0 will take roughly the same amount of time to execute as when value is 9. Other browsers, however, aren’t nearly as good. Internet Explorer, Opera, Safari, and Chrome all show noticeable increases in the execution time as you get deeper into the switch statement. Those increases, however, are smaller than the increases experienced with each additional condition of an if statement. You can therefore improve the performance of switch statements by ordering the conditions in decreasing rate of frequency (the same as if statement optimization).

所以我写了一个if语句!但等等。

优化 If 语句

而不是写:

if (value == 0){
return result0;
} else if (value == 1){
return result1;
} else if (value == 2){
return result2;
} else if (value == 3){
return result3;
} else if (value == 4){
return result4;
} else if (value == 5){
return result5;
} else if (value == 6){
return result6;
} else if (value == 7){
return result7;
} else if (value == 8){
return result8;
} else if (value == 9){
return result9;
} else {
return result10;
}

你可以写这样的东西,所以最坏的情况并不像评估 9 个条件那么糟糕:

if (value < 6){

if (value < 3){
if (value == 0){
return result0;
} else if (value == 1){
return result1;
} else {
return result2;
}
} else {
if (value == 3){
return result3;
} else if (value == 4){
return result4;
} else {
return result5;
}
}

} else {

if (value < 8){
if (value == 6){
return result6;
} else {
return result7;
}
} else {
if (value == 8){
return result8;
} else if (value == 9){
return result9;
} else {
return result10;
}

}

来源:http://archive.oreilly.com/pub/a/server-administration/excerpts/even-faster-websites/writing-efficient-javascript.html

关于Javascript:switch-case 结构是检查大量正则表达式的最快方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38868276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com