Javascript:什么更有效,IF块或TRY / CATCH? [英] Javascript: What's more efficient, IF block or TRY/CATCH?

查看:174
本文介绍了Javascript:什么更有效,IF块或TRY / CATCH?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此代码中效率更高的其他一些看法很感兴趣。基本上在下面的代码中,有一个setInterval循环,我需要在代码在循环中运行之前满足4个要求。所以在第1节我写了一个if语句检查所有4.工作正常。

I'd love some other opinions on what's more efficient in this code. Basically in the following code, there's a setInterval loop and I need 4 requirements to be true before the code runs in the loop. So in v.1 I wrote an if statement checking all 4. Worked fine.

然后我切换到只使用try / catch,我想要执行的代码在试试{}。逻辑是在每个循环期间,将生成异常但针对每个无效条件进行抑制。在所有条件都为真的最后一个循环中,代码执行并清除间隔。

Then I switched to just using try/catch, with the code I want to execute sitting in try{}. The logic was that during each loop, an exception would be generated but suppressed for each invalid condition. On the final loop where all conditions are true, the code executes and clears the interval.

两者都有效。我喜欢try / catch方法,因为我需要编写更少的条件代码并担心破坏。但是我担心try / catch的效率非常低,特别是在一个100ms的setInterval()循环中。在这里有什么对其他聪明人的看法?

Either works. I like the try/catch method because there's less conditional code that I need to write and worry about breaking. But I'm worried that try/catch is really inefficient, especially in a setInterval() loop hitting at 100ms. What are some opinions of other bright minds here on SO?

尝试/捕获

var intvar = setInterval(function(){
try{    
    clearInterval(intvar);

    jQuery('#'+nav[pageid].t1+'>a').replaceWith(jQuery('<span>'+jQuery('#'+nav[pageid].t1+'>a').text()+'</span>'));

    //set display classes for nav
    jQuery('#'+nav[pageid].t1).addClass('selected').find('#'+nav[pageid].t2).addClass('subselect'); //topnav
    jQuery('#'+nav[pageid].t3).addClass('selected').find('#'+nav[pageid].t4).addClass('subselect'); //leftnav
}catch(err){}
},100);

IF Block

var intvar = setInterval(function(){

if(typeof jQuery == 'function' && typeof nav == 'object' && typeof pageid != 'undefined' && typeof document.getElementById('leftnav') == 'object'){
    clearInterval(intvar);
    jQuery('#'+nav[pageid].t1+'>a').replaceWith(jQuery('<span>'+jQuery('#'+nav[pageid].t1+'>a').text()+'</span>'));

    //set display classes for nav
    jQuery('#'+nav[pageid].t1).addClass('selected').find('#'+nav[pageid].t2).addClass('subselect'); //topnav
    jQuery('#'+nav[pageid].t3).addClass('selected').find('#'+nav[pageid].t4).addClass('subselect'); //leftnav
}

},100);


推荐答案

使用if语句。我不知道TRY / CATCH的开销是多少,但我怀疑它比评估布尔表达式要大得多。要点击TRY / CATCH,你必须:执行一个语句,生成一个错误[带有相关的开销],记录错误(大概),做一个堆栈跟踪(大概),并移回代码。另外,如果你必须在这些行附近调试代码,真正的错误可能会被你的TRY / CATCHing混淆。

Use the if statement. I don't know what the overhead is for a TRY/CATCH, but I suspect it's far greater than evaluating a boolean expression. To hit the TRY/CATCH you will have to: execute a statement, generate an error [with that associated overhead], log the error (presumably), made a stacktrace(presumably), and moved back into the code. Additionally, if you have to debug code near those lines the real error could get obfuscated with what you are TRY/CATCHing.

此外,它是滥用TRY / CATCH和可以使您的代码更难阅读。假设您为更长或更多混淆的案例执行此操作?您的捕获量最终会在哪里?

Furthermore, it's a misuse of TRY/CATCH and can make your code that much harder to read. Suppose you do this for longer or more obfuscated cases? Where might your catch end up?

这被称为异常处理

编辑:如下所述,如果实际导致异常,则仅获取运行时性能命中。

As commented below, you only take the runtime performance hit if you actually cause an exception.

这篇关于Javascript:什么更有效,IF块或TRY / CATCH?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆