我正在使用 jquery mobile+phonegap 构建本地/静态移动应用程序。由于我想尽量减少对服务器端的依赖,因此我想放置结果文件(至少 200 多个具有单独表格结果的相同外观页面)。再想一想,这肯定会增加应用程序的大小。因此,我得出的结论是,只有一个带有条件结果的结果页面。问题是,大约有。 200 多个结果。
因此,我有点困惑该走哪条路。如果必须选择条件语句,则选择哪一个才能获得更好的性能,因为我当然可能会增加应用程序的结果加载,在这种情况下,
- if else 阶梯,
喜欢
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>;
或者 - 开关盒?
喜欢
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;
}
甚至包含不同的结果页面,如 result1.html、result2.html、result3.html。
作为本地存储的智能手机应用程序,可以提供更好的性能(三者之一)
最佳答案
如果我正确理解你的问题,你正在尝试在 else/if 梯子和 switch 语句之间做出决定。 switch 语句对 CPU 的占用较少,是两者之间的最佳选择。该代码将使用 switch 语句进行少量比较,而如果使用 if/else 梯形图,则平均会进行 100 次比较。
switch(someVar){
case 1:
<handle case 1>
break;
case 2:
<handle case 2>
break;
case 3:
<handle case 3>
break;
.
.
.
}
这可能会导致进行的比较少得多,并有望同时处理多个案例。并且还可以防止您意外地发现您不想发现的案件。
关于javascript - 更好的性能?在 Javascript/Jquery Mobile 中一次即可生成 200 多个单独的结果页面或 200 多个条件(如果是梯子或 switch 情况?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19603814/