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

标签 javascript switch-statement

为了解决(正则表达式)检测多个条目的问题,从可读性和功效(速度、内存使用)的 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/

相关文章:

javascript - 动态家谱遍历 php javascript

javascript - 使用 Canvas 作为多个范围输入的垫

javascript - 修改单页滚动功能 - 重叠而不是向上滑动

javascript - 关于 JavaScript : how can i "reset" switch statement? 的问题

JavaScript:针对冗余进行优化(循环 + 开关 + 键/值)

javascript - 添加到已设置 <a href/> 的图像的链接?

javascript - 如何使用ajax检索JSON文件数据以及当用户单击按钮时内容显示在表中?

ios - 在 tableview cellForRowAtIndexPath 中切换

ios - 太多样板代码 switch 语句 - iOS

C++:为什么我们可以在 switch 的情况下声明变量