Javascript performance: switch or if nested?

9

Which of the two alternatives provides the best performance in Javascript: switch or if nested?

if {
  ...
} else {
  if {
    ...
  } else {
    if {
      ...
    } else {
      ...
    }
  }
}

or

switch(expression) {
    case n:
        code block
        break;
    case n:
        code block
        break;
    default:
        default code block
}
    
asked by anonymous 20.06.2014 / 18:14

1 answer

8

As incredible as it may seem, else-if is the most performative of the possible options.

This and in this benchmark, we've seen that Chrome performs better in terms of% almost regardless of browser version.

The benchmark is a technical appraiser. By logic, else-if is a simple and explicit condition, while else-if , however trivial it may seem, has (more) complex criteria behind it - it is sensitive and intelligent because it make implicit decisions, such as the switch controller and the default value. A% of%, for example, can not, of course, determine a standard case - you must do this without subjection; you should do this by explicitly indicating in your code.

Simply put, case has a more complex algorithm behind it so you can make some decisions without having to give explanations. He is more autonomous; more independent. else-if does this only if someone else says so.

See the following example :

var foo = 1;
switch (foo) {
    case 0:
    case 1:
    case 2:
    case 3:
        alert('yes');
        break;
    default:
        alert('not');
}

Even though we do not use switch , as in the following example, the implementation of the else-if function will be prepared if we are going to use it. So it is convex - if we do not use something, it will be there anyway, and that requires hardware; that requires arm. See:

var foo = 1;
switch (foo) {
    case 0:
    // [...]
}

A default , on the other hand, does not have these "wild cards". He will only treat what you tell him to do, otherwise he is hostile. In short, even with switch , there are no prepared things that can "grease" your solution if you do not use it - and that makes it simpler and almost which consequently more performative. >

Practical contextualization

The else-if has the reserved keys else-if , switch and et cetera. If we stop to think, he needs to know what to do when the developer calls for case and when to assign conditions to default s.

If the developer does not assign anything to default , no problem! Does it have a standard of treatment when it does - and what does that pattern demand? More code, more logic and finally more performance . As I said, case is more objective and "dumb" - it needs you to explain everything you should do because it is not smart enough to make autonomous decisions, which diverges from default . >

About JavaScript processors

As you can see, the benchmark mentioned earlier is Google's Chrome browser, which has the V8 engine as processor.

Each engine has its own compilation mechanism, and these mechanisms may assume more or less performative commitments depending on their structuring and implementation. The point I want to get is that in the case of Chrome, else-if is "unbeatable" because in the dressing rooms things work in a specific way that gives that merit to it.

On the other hand, in this benchmark , it evaluates how%% of% a higher performance option if related to switch s strings in Firefox. Why this? Compiler. More than the implementation of else-if there, the philosophy of development is another.

switch or if , anyway?

In my opinion, because Chrome being used more than Firefox and any other browser , I would of switch in the performance theme.

The else-if , in some cases, is more semantic and its applicability stands out: if your search is this - which escapes the scope of the question - then go with it.

Potential lies

To strengthen my response, I anticipate something that the user mentioned in the comments: Benchmarks are extremely relative , especially of modern browsers. If you look at the benchmarks I've shared, it's easy to spot a big swing in the results across browser versions.

Where I want to get is: not that benchmarks are not reliable, but they are not totally reliable . As I replied to Maniero, the ideal is to use statistics as a base and reference, but not as the ultimate solution to peace.

    
20.06.2014 / 18:30