Incredible as it may seem, the else-if
is the most performatic among the possible options.
In this and in this benchmark, we analyze that Chrome performs better else-if
almost browser version independent.
The benchmark is a technical evaluator. Logically, the else-if
is a simple and explitic condition, while the switch
, as trivial as it may seem, it has complex (more) criteria behind it - it is sensitive and intelligent because it can make implicit decisions, as is the case of the controller default
and the case
worthless. A else-if
, for example, you cannot, of course, determine a default case - you must do this without subjection; you must do this by explicitly indicating in your code.
Simply put, the switch
has a more complex algorithm behind it to enable it to make some decisions without having to explain. It is more autonomous; more independent. The else-if
does so only if someone 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 if we don’t use the default
, as in the following example, the implementation of the switch
will be ready if we use it. So it’s convex - if we don’t use something, it’s there anyway, and it requires hardware; it requires arm. See:
var foo = 1;
switch (foo) {
case 0:
// [...]
}
A else-if
, on the other hand, does not possess these "jokers". He will only treat what you tell him to do, otherwise he is hostile. In short, still at else-if
, there are no prepared things that can "fatten" your solution if you don’t use it - and this makes it simpler and almost that consequently more performatic.
Practical contextualization
The switch
holds the keys reserved case
, default
e et cetera. If we stop to think, it needs to know what to do when the developer calls for default
and when to grant conditions to case
s.
If the developer does not assign anything to default
, no problem! It has a standard of treatment when this occurs - and what does this standard demand? More code, more logic and finally more performance. Like I said, the else-if
is more objective and "dumb" - he needs you to explain everything you should do because he is not smart enough to make autonomous decisions, which diverges from a switch
.
About Javascript processors
As you can see, the previously mentioned benchmark consists of Google’s browser, Chrome, which has the V8 engine as a processor.
Each engine has its own compilation mechanism, and these mechanisms can assume more or less performatic commitments depending on their structure and implementation. The point I want to make is that in the case of Chrome, else-if
is "unbeatable" because in the dressing rooms things work in a specific way that gives this merit to him.
On the other hand, in this benchmark, he evaluates how the switch
a better performance option if related to if
s in Firefox. Why this? Compiler. More than the implementation of switch
there, the philosophy of development is another.
else-if
or switch
, after all?
In my opinion, for the fact of Chrome be used more than Firefox and any other browser, I would go from else-if
on the theme performance.
The switch
, in some cases, it is more semantic and its applicability stands out: if your search is this - what escapes the scope of the question -, then go with it.
Potential lies
To strengthen my response, I anticipate something that the user Maniero mentioned in the comments: benchmarks are extremely relative, mainly from modern navigators. If you look at the benchmarks I’ve shared, it’s easy to spot a very large oscillation in results throughout browser versions.
My point is: not that benchmarks are unreliable, but they are not fully trusted. As I replied to Maniero, the ideal is to use statistics as a little base and reference, but not as the definitive solution to peace.
Why did they deny his question? It is not broad and easily explained with benchmarks, without having a basis in opinions. I was positive, the question is relevant. =)
– Guilherme Oderdenge
I did not negatively, but the benchmark could be the assumption of the question. And the question is why the results.
– bfavaretto
What is the question after all? the only phrase that exists in the post does not tell me exactly what the question is, as intuitive as it is. It could be, which of the two is more beautiful in the code, which of the two is slower, which of the two is faster, which of the two is more used... anyway. For that reason I have denied.
– Marciano.Andrade
@Marciano I edited to make it clearer, but even before I spoke about performance.
– user7261