<tfoot id='o5NWN'></tfoot>
  • <small id='o5NWN'></small><noframes id='o5NWN'>

      <bdo id='o5NWN'></bdo><ul id='o5NWN'></ul>
    <legend id='o5NWN'><style id='o5NWN'><dir id='o5NWN'><q id='o5NWN'></q></dir></style></legend>
      1. <i id='o5NWN'><tr id='o5NWN'><dt id='o5NWN'><q id='o5NWN'><span id='o5NWN'><b id='o5NWN'><form id='o5NWN'><ins id='o5NWN'></ins><ul id='o5NWN'></ul><sub id='o5NWN'></sub></form><legend id='o5NWN'></legend><bdo id='o5NWN'><pre id='o5NWN'><center id='o5NWN'></center></pre></bdo></b><th id='o5NWN'></th></span></q></dt></tr></i><div id='o5NWN'><tfoot id='o5NWN'></tfoot><dl id='o5NWN'><fieldset id='o5NWN'></fieldset></dl></div>

        if-else、switch 或 map based 条件的性能

        时间:2023-09-07

        <legend id='daNUb'><style id='daNUb'><dir id='daNUb'><q id='daNUb'></q></dir></style></legend>

          1. <tfoot id='daNUb'></tfoot>

                <bdo id='daNUb'></bdo><ul id='daNUb'></ul>
              • <i id='daNUb'><tr id='daNUb'><dt id='daNUb'><q id='daNUb'><span id='daNUb'><b id='daNUb'><form id='daNUb'><ins id='daNUb'></ins><ul id='daNUb'></ul><sub id='daNUb'></sub></form><legend id='daNUb'></legend><bdo id='daNUb'><pre id='daNUb'><center id='daNUb'></center></pre></bdo></b><th id='daNUb'></th></span></q></dt></tr></i><div id='daNUb'><tfoot id='daNUb'></tfoot><dl id='daNUb'><fieldset id='daNUb'></fieldset></dl></div>
              • <small id='daNUb'></small><noframes id='daNUb'>

                    <tbody id='daNUb'></tbody>
                  本文介绍了if-else、switch 或 map based 条件的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想知道以下 javascript 中条件结构的实现的性能.

                  I was wondering about the performances of the following implementations of conditional structs in javascript.

                  方法一:

                   if(id==="camelCase"){
                      window.location.href = "http://www.thecamelcase.com";
                  }else if (id==="jsFiddle"){
                      window.location.href = "http://jsfiddle.net/";
                  }else if (id==="cricInfo"){
                      window.location.href = "http://cricinfo.com/";
                  }else if (id==="apple"){
                      window.location.href = "http://apple.com/";
                  }else if (id==="yahoo"){
                      window.location.href = "http://yahoo.com/";
                  }           
                  

                  方法二:

                  switch (id) {
                  case 'camelCase':
                      window.location.href = "http://www.thecamelcase.com";
                      break;
                  case 'jsFiddle':
                      window.location.href = "http://www.jsfiddle.net";
                      break;
                  case 'cricInfo':
                      window.location.href = "http://www.cricinfo.com";
                      break;
                  case 'apple':
                      window.location.href = "http://www.apple.com";
                      break;
                  case 'yahoo':
                      window.location.href = "http://www.yahoo.com";
                      break;
                  
                  }
                  

                  方法3

                  var hrefMap = {
                  camelCase : "http://www.thecamelcase.com",
                  jsFiddle: "http://www.jsfiddle.net",
                  cricInfo: "http://www.cricinfo.com",
                  apple: "http://www.apple.com",
                  yahoo: "http://www.yahoo.com"
                  };
                  window.location.href = hrefMap[id];
                  

                  方法四

                  window.location.href = {
                      camelCase : "http://www.thecamelcase.com",
                      jsFiddle: "http://www.jsfiddle.net",
                      cricInfo: "http://www.cricinfo.com",
                      apple: "http://www.apple.com",
                      yahoo: "http://www.yahoo.com"
                  }[id];
                  

                  可能方法 3 和 4 可能具有几乎相同的性能,但只是发布以确认.

                  Probably Method 3 and 4 might have almost the same performance but just posting to confirm.

                  推荐答案

                  据此JSBen.ch测试,switch 设置是提供的方法中最快的(Firefox 8.0 和 Chromium 15).

                  According to this JSBen.ch test, the switch setup is the fastest out of the provided methods (Firefox 8.0 and Chromium 15).

                  方法 3 和 4 的速度稍慢,但几乎不明显.显然,if-elseif 方法的速度要慢得多(FireFox 8.0).

                  Methods 3 and 4 are slightly less fast, but it's hardly noticeable. Clearly, the if-elseif method is significantly slower (FireFox 8.0).

                  Chromium 15 中的相同测试未显示这些方法之间的性能存在显着差异.事实上,if-elseif 方法似乎是 Chrome 中最快的方法.

                  The same test in Chromium 15 does not show significant differences in performance between these methods. In fact, the if-elseif method seems to be the fastest method in Chrome.

                  我已经再次运行了测试用例,另外还有 10 个条目.hrefmap(方法 3 和 4)表现出更好的性能.

                  I have run the test cases again, with 10 additional entries. The hrefmap (methods 3 and 4) show a better performance.

                  如果你想在函数中实现 compare 方法,方法 3 肯定会胜出:将映射存储在一个变量中,然后在稍后引用这个变量,而不是重新构建它.

                  If you want to implement the compare method in a function, method 3 would definitely win: Store the map in a variable, and refer to this variable at a later point, instead of reconstructing it.

                  这篇关于if-else、switch 或 map based 条件的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:用于比较大于或小于数字的值的 switch 语句 下一篇:从Javascript中的switch case内部中断for循环

                  相关文章

                  <legend id='B1LO6'><style id='B1LO6'><dir id='B1LO6'><q id='B1LO6'></q></dir></style></legend>

                    <tfoot id='B1LO6'></tfoot>
                  1. <small id='B1LO6'></small><noframes id='B1LO6'>

                    • <bdo id='B1LO6'></bdo><ul id='B1LO6'></ul>
                      <i id='B1LO6'><tr id='B1LO6'><dt id='B1LO6'><q id='B1LO6'><span id='B1LO6'><b id='B1LO6'><form id='B1LO6'><ins id='B1LO6'></ins><ul id='B1LO6'></ul><sub id='B1LO6'></sub></form><legend id='B1LO6'></legend><bdo id='B1LO6'><pre id='B1LO6'><center id='B1LO6'></center></pre></bdo></b><th id='B1LO6'></th></span></q></dt></tr></i><div id='B1LO6'><tfoot id='B1LO6'></tfoot><dl id='B1LO6'><fieldset id='B1LO6'></fieldset></dl></div>