Learner Always
Learner Always

Reputation: 1946

Better performance? 200+ individual result pages or 200+ conditions (if else ladder or switch case?) in a single go in Javascript/Jquery Mobile

I am building a local/static mobile app using jquery mobile+phonegap. Since I want to be least dependent on getting server side, I thought to go for placing the resultant files (at least 200+ same looking pages with individual table results). For a second thought, that would certainly increase the app's size. So I came to a conclusion on having a single resultant page with conditional results. Problem is, there are approx. 200+ results.

Hence, I'm a little confused which way to go. If a conditional statement has to be chosen, which one to go with for a better performance as I certainly might be increasing the result loading of the app, in this case,

like

if(someVar==1)
  <//Display so and so variable and image values at placeholders and header on result.html>;

else if(someVar==2)
  <//Display so and so variable and image values at placeholders and header on result.html>;

else if(someVar==3)
  <//Display so and so variable and image values at placeholders and header on result.html>;
.
.
.
.
else if(someVar==200)
  <//Display so and so variable and image values at placeholders and header on result.html>;

or - the switch case?

like

switch(someVar){
  case 1: <//Display so and so variable and image values at placeholders and header on result.html>;
          break; 
  case 2: <//Display so and so variable and image values at placeholders and header on result.html>;
    break;
  case 3: <//Display so and so variable and image values at placeholders and header on result.html>;
    break;
  .
  .
  .
  case 200: <//Display so and so variable and image values at placeholders and header on result.html>;
    break;

}

or even containing different result pages like result1.html, result2.html, result3.html.

Whatever could give better performance as a locally stored smartphone app (either of the 3)

Upvotes: 0

Views: 117

Answers (2)

distributed
distributed

Reputation: 396

You can of course also save all your data into an Array or an object and then retrieve individual elements via

dipslayData(data[somevar])

with a suitable displayDatafunction. This might be more efficient or readable than the switch. It might also be more readable than the switch, because you can separate the behaviour of what you do with your "variables" and where you define them.

In the end, though, you should make performance comparisons out of thin air. First, you should know that you really need the performance. Secondly, you should profile, i.e. measure how long the parts of your program take. Based on this, you can optimize your hottest, most optimization-worthy functions. Some web browsers such as Google Chrome have built in profiling tools.

And profiling is awesome and fun :)

Upvotes: 1

Ryan
Ryan

Reputation: 96

If I understand your question correctly, you are trying to decide between an else/if ladder and a switch statement. The switch statement will be less cpu intensive and your all around best option between the two. The code will make a small handful of comparisons with a switch statement versus 100 comparisons on average if you use an if/else ladder.

switch(someVar){
  case 1:
    <handle case 1>
    break;
  case 2:
    <handle case 2>
    break;
  case 3:
    <handle case 3>
    break;
  .
  .
  .
}

This will probably result in far less comparisons being made and hopefully handling multiple cases at once. And also prevents you from accidentally catching a case you didn't intend to catch.

Upvotes: 1

Related Questions