简体   繁体   English

使用object literal而不是switch语句

[英]Using object literal rather than switch statement

There is a lot of discussions and comments about best practices in switch and if/else statements. 关于switchif / else语句的最佳实践有很多讨论和评论。 I have seen people saying that we all should use object literal instead of switch when it is possible. 我看到人们说我们都应该在可能时使用对象文字而不是切换

So I've reproduced this case and I went trough a little and simple test between a switch : 所以我重现了这个案例, 在交换机之间进行了一些简单的测试

(function(test){
    var bar;

    switch(bar) {
        case 1:
            bar = 'red';
            break;
        case 2:
            bar = 'blue';
            break;
        case 3:
            bar = 'yellow';
            break;
        case 4:
            bar = 'green';
            break;
        case 5:
            bar = 'black';
            break;
    }

    return bar;
})(5);

and passing trough an object literal : 并通过一个对象文字:

(function(test){
    return { 1: 'red', 2: 'blue', 3: 'yellow', 4: 'green', 5: 'black' }[ test ];
})(5);

After running this test , it seems evident that the switch statement is faster than calling the tested value in an object literal. 运行此测试后 ,很明显switch语句比在对象文字中调用测试值更快。

Is my test wrong ? 我的考试错了吗? What should I consider before using one or another method in this case ? 在这种情况下使用一种或另一种方法之前我应该​​考虑什么? Or maybe opinionated comments I've seen about this subject are just wrong and I should not try to bypass fundamentals... 或者也许我看过关于这个主题的看法评论是错误的,我不应该试图绕过基本面......

You're likely seeing effects of scale: A switch statement is O(n) , while a hash table lookup (presumably used to find methods in object literals) is (amortized) O(1) . 您可能会看到规模效应:switch语句是O(n) ,而哈希表查找(可能用于查找对象文字中的方法)是(摊销) O(1) But Big-O measures only accurately describe how performance scales over really big inputs. 但是,Big-O指标只能准确地描述绩效如何超过真正的大投入。 In your test, it's not surprising that five if statements are faster than a hash table lookup. 在您的测试中,五个if语句比哈希表查找更快并不奇怪。

So basically: How many keys are you going to have? 所以基本上: 你将拥有多少把钥匙? If you've only got five keys, you'll hit on average 2.5 per lookup, which you've shown to be faster than a single hash table lookup. 如果你只有五个密钥,那么每个查询的平均命中率为2.5,你已经证明它比单个哈希表查找要快。 But what if you have 500? 但是如果你有500个怎么办? That's an average of 250 if statements - still versus a single hash lookup. 这是平均250个if语句 - 仍然是单个哈希查找。 The hash table (object literal) approach is almost assuredly better at that point. 在这一点上,哈希表(对象文字)方法几乎肯定更好。

But the ultimate answer? 但最终答案呢? everybody hates to hear this, but it's the only authoritative way: Do the benchmark with your actual production code. 每个人都讨厌听到这个,但这是唯一的权威方式:用你的实际生产代码做基准测试。 It's a pain, but then you know for sure. 这是一种痛苦,但你肯定知道。

Hope that helps! 希望有所帮助!

PS: This is leaving aside all considerations of coding style preference, but I really don't want to get into that... PS:这是不考虑编码风格偏好的所有考虑因素,但我真的不想进入...

Well I modified your code ( here ) for you to see when object notion would be better. 好吧,我修改了你的代码( 这里 ),让你看看对象的概念会更好。

Just as Xavier described, but it didn't have to go to about 500. Roughly 40 cases exposed the switch.. 正如Xavier描述的那样,但它不必大约500个。大约40个案例暴露了开关..

Another thing, besides the size of the list to note is that, in object notion the lookup for an integral would gain no benefit, while switch can optimize that bit. 另外一点,除了要注意的列表的大小之外,在对象概念中,查找积分将不会获益,而切换可以优化该位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用对对象字面量的多个赋值替换 switch 语句 - Replace a switch statement with multiple assignments to object literal 引用类/实例而不是 object 文字属性的正确方法? - Correct way to refer to class/instance rather than object literal property? 为什么将数据存储在JSON中而不是JavaScript对象文字/初始化程序中? - Why store data in JSON rather than JavaScript object literal/initializer? Angular - 是否可以执行switch语句而不是多个@Input()? - Angular - Is it possible to perform a switch statement rather than multiple @Input()s? 是否可以将包含真实值的switch语句写为对象文字查找? - Can a switch statement containing truthy values be written as an object literal lookup? 使用对象替换switch语句 - Using an object to replace the switch statement 将某些东西解析为 object 文字而不是块有什么区别? - What difference does it makes to parse something as an object literal rather than as a block? 使用for循环而不是大型的if语句通过jQuery附加信息 - using a for loop rather than a large if statement to append info via jQuery 使用类名和 switch 语句返回 object - Return object using classnames and switch statement 使用replaceState()(或其他方式)更新状态对象,而不是替换它 - Using replaceState() (or something else) to update the state object rather than replace it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM