使用object literal而不是switch语句 [英] Using object literal rather than switch statement

查看:125
本文介绍了使用object literal而不是switch语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

切换 if / else 语句中,有很多关于最佳做法的讨论和评论。我看到有人说我们都应该在可能的时候使用object literal而不是switch

There is a lot of discussions and comments about best practices in switch and if/else statements. 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);

并通过对象字面值:

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

运行此测试,很明显switch语句比在对象文字中调用测试值更快。

After running this test, it seems evident that the switch statement is faster than calling the tested value in an object literal.

我的测试错了?在这种情况下使用一种或另一种方法之前我应该​​考虑什么?或者我看过关于这个主题的看法评论是错误的,我不应该试图绕过基本面...

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...

推荐答案

您可能会看到规模效应:switch语句是 O(n),而哈希表查找(可能用于查找对象文字中的方法)是(已摊销) O(1)。但是,Big-O指标只能准确地描述绩效如何超过真正的大投入。在你的测试中,五个 if 语句比哈希表查找更快并不奇怪。

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). But Big-O measures only accurately describe how performance scales over really big inputs. In your test, it's not surprising that five if statements are faster than a hash table lookup.

所以基本上: 你将拥有多少个密钥?如果你只有五个密钥,那么每个查询的平均命中率为2.5,你已经证明它比单个哈希表查找要快。但是如果你有500个怎么办?如果语句,这仍然是250 的平均值 - 仍然是哈希查找。哈希表(对象文字)方法在这一点上几乎肯定更好。

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. But what if you have 500? That's an average of 250 if statements - still versus a single hash lookup. 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.

希望有所帮助!

PS: 这暂不考虑编码风格偏好的所有考虑因素,但我真的不想进入那个......

PS: This is leaving aside all considerations of coding style preference, but I really don't want to get into that...

这篇关于使用object literal而不是switch语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆