qweweretrt515
V2EX  ›  问与答

javascript 对同一个变量多次判断的时候,用多个 if 好,还是 if+多个 else if 好?

  •  
  •   qweweretrt515 · Mar 6, 2017 · 1021 views
    This topic created in 3382 days ago, the information mentioned may be changed or developed.

    方案 1 :

     if(a==z){} 
     if(a>x){} 
     if(a<c){}
     if(a==d){}
    

    方案 2 :

     if(a==z){}
     else if(a>x){}
     else if(a<c){}
     else if(a==d){}
    

    哪个性能更好呢? 大家觉得用哪个好呢

    SoloCompany
        1
    SoloCompany  
       Mar 6, 2017   ❤️ 1
    问你的业务逻辑,不要问性能
    业务逻辑等价的话,用后者(如果语言支持的话, switch case 更佳)
    不等价的话,你这个问题还有提问的必要吗
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   931 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 21:19 · PVG 05:19 · LAX 14:19 · JFK 17:19
    ♥ Do have faith in what you're doing.