[removed] What's more efficient, IF block or TRY/CATCH?

前端 未结 7 1253
忘了有多久
忘了有多久 2020-12-14 08:55

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 befor

7条回答
  •  自闭症患者
    2020-12-14 09:16

    To directly answer the question, as everyone else has, the try..catch is likely going to be more expensive if there actually is an error.

    To point out some additional errors in the code beyond what others have already pointed out:

    These two codes are not at all equivalent. To explain, even though the codes appear to do the exact same thing, they do not.

    In the case of the if() check, NONE of the code is executed. In the case of the exception handler, each line of code inside the exception handler will be executed. SO, what happens if the error occurs in the second, or the third line? Then you've got something completely different happening in your code than what you get if you check the conditions before executing any of it.

提交回复
热议问题