最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JSON like syntax vs switch statement javascript - Stack Overflow

programmeradmin2浏览0评论

I saw a post on / where he talks about an alternative way to using switch statements.

I have created a snippet below, but I'm not sure why the alternate is 99% slow.

function doX(){}
function doY(){}
function doN(){}
var something = 1;

var cases = {
    1: doX,
    2: doY,
    3: doN
};
if (cases[something]) {
    cases[something]();
}

Any idea?

I saw a post on http://www.jquery4u./javascript/shorthand-javascript-techniques/ where he talks about an alternative way to using switch statements.

I have created a snippet below, but I'm not sure why the alternate is 99% slow.

function doX(){}
function doY(){}
function doN(){}
var something = 1;

var cases = {
    1: doX,
    2: doY,
    3: doN
};
if (cases[something]) {
    cases[something]();
}

http://jsperf./alternateswitch

Any idea?

Share Improve this question edited Aug 20, 2013 at 16:35 bfavaretto 71.9k18 gold badges117 silver badges159 bronze badges asked Aug 20, 2013 at 16:22 ArunArun 1,0171 gold badge12 silver badges26 bronze badges 4
  • Just like the shorthand version of if/else he is talking about, it is shorter, but it isn't faster at all. – Romain Braun Commented Aug 20, 2013 at 16:26
  • 2 That "JSON" syntax is just an object. – Blender Commented Aug 20, 2013 at 16:27
  • 6 I also think a large part of your difference in results is a misuse of jsperf. Make sure to add any one time code to the setup portion of the tests. Here is an example using your code: jsperf./alternateswitch/3 – Chris B Commented Aug 20, 2013 at 16:28
  • 1 Note that not all switch patterns translate easily (if you omit the break statement in a switch, execution falls through to the next case) – SheetJS Commented Aug 20, 2013 at 16:36
Add a ment  | 

4 Answers 4

Reset to default 8

That "JSON" syntax is just an object. Also, your parison is a little unfair here, as you create a brand new object every single timed loop, which is somewhat expensive.

If you move the object creation to the setup section, the speed difference bees neglibile: http://jsperf./alternateswitch/4

If you remove the if statement, the object will be a tad faster (at least for me): http://jsperf./alternateswitch/5. The extra property lookup and truthiness check does slow it down.

The author never claimed the shorter code, which is just a hash map of the possible cases, would actually be faster. Obviously, the array creation negatively impacts the performance when you run it in a test suite. At the same time, the switch statement is piled code.

You will see some improvement if your code is being reused, i.e. you keep the value of cases; I've measured a difference of about 20-30% in this test case, depending on which case occurs more often.

That said, an isolated performance test such as this won't be useful unless your code is run inside a tight loop, because the test cases run at 50M+ operations per second on my home puter. Differences between the two should therefore be based on other factors, such as code clarity or the fact that switch statements are easy to mess up if you forget to place break; a statement.

  • I believe that Javascript object is an associative array which usually implemented as a hash table. Each lookup requires a key to go through a hashing function. Hashing function is like a double blade. For a small size data, it would be slower than a if-elseif-else. However, for larger data, it will outperform ordinary if-elseif-else
  • It is very unfair that you are favoring the switch, you make that the variable you are looking for is at the first case. Therefore, the plexity for switch is O(1) for your testing.

Usually switch statements are optimized by the piler/interpreter. They are even faster than chained if-else statements. By using a JSON object instead of switch statement, you're bypassing the Javascript engine optimization.

发布评论

评论列表(0)

  1. 暂无评论