Javascript switch vs. if…else if…else

Javascript switch vs. if…else if…else

Guys I have a couple of questions:

Is there a performance difference in JavaScript between a switch statement and an if…else?
If so why?
Is the behavior of switch and if…else different across browsers? (FireFox, IE, Chrome, Opera, Safari)

The reason for asking this question is it seems that I get better performance on a switch statement with approx 1000s cases in Firefox.

Edited
Unfortuantly this is not my code the Javascript is being produced serverside from a compiled library and I have no access to the code. The method that is producing the javascript is called
CreateConditionals(string name, string arrayofvalues, string arrayofActions)

note arrayofvalues is a comma separated list.
what it produces is
function [name] (value) {
if (value == [value from array index x]) {
[action from array index x]
}
}

Note: where [name] = the name passed into the serverside function
Now I changed the output of the function to be inserted into a TextArea, wrote some JavaScript code to parse through the function, and converted it to a set of case statements.
finally I run the function and it runs fine but performance differs in IE and Firefox.

Related:  Getting selected text position

Solutions/Answers:

Solution 1:

Answering in generalities:

  1. Yes, usually.
  2. See More Info Here
  3. Yes, because each has a different JS processing engine, however, in running a test on the site below, the switch always out performed the if, elseif on a large number of iterations.

Test site

Solution 2:

Sometimes it’s better to use neither. For example, in a “dispatch” situation, Javascript lets you do things in a completely different way:

function dispatch(funCode) {
  var map = {
    'explode': function() {
      prepExplosive();
      if (flammable()) issueWarning();
      doExplode();
    },

    'hibernate': function() {
      if (status() == 'sleeping') return;
      // ... I can't keep making this stuff up
    },
    // ...
  };

  var thisFun = map[funCode];
  if (thisFun) thisFun();
}

Setting up multi-way branching by creating an object has a lot of advantages. You can add and remove functionality dynamically. You can create the dispatch table from data. You can examine it programmatically. You can build the handlers with other functions.

There’s the added overhead of a function call to get to the equivalent of a “case”, but the advantage (when there are lots of cases) of a hash lookup to find the function for a particular key.

Related:  Is there any memory limit for Google Chrome browser?

Solution 3:

The performance difference between a switch and if...else if...else is small, they basically do the same work. One difference between them that may make a difference is that the expression to test is only evaluated once in a switch while it’s evaluated for each if. If it’s costly to evaluate the expression, doing it one time is of course faster than doing it a hundred times.

The difference in implementation of those commands (and all script in general) differs quite a bit between browsers. It’s common to see rather big performance differences for the same code in different browsers.

As you can hardly performance test all code in all browsers, you should go for the code that fits best for what you are doing, and try to reduce the amount of work done rather than optimising how it’s done.

Solution 4:

  1. If there is a difference, it’ll never be large enough to be noticed.
  2. N/A
  3. No, they all function identically.
Related:  How to add Drop-Down list (

Recent Posts

Categories

Recent Comments