Possible Duplicate:
Javascript Try-Catch Performance Vs. Error Checking Code
A colleague of mine told me that using a lot of try catch blocks in javascript is going to be a hit in performance . Is that claim true and if yes then why ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Short answer, yes he is correct. A
try/catchstatement has do to certain things under the hood in order to work. For instance, it will extend the scope lookup chain (which might not be that big problem in modern environments) but it especially needs to evaluate whatever code is within thattry-block, to figure if there are any errors thrown.Anyway,
try/catchblocks are a great opportunity to make your code stable. However, you should always apply it on the smallest fraction of your code possible and of course only, if it is really needed (like, avoiding browser specific bugs/errors which you can’t work around). Just putting like all of your script into one bigtry/catchblock, is certainly the worst idea.