In Javascript, is it expensive to use try-catch blocks even if an exception is never thrown?
Are you doing typical CRUD UI code? Use try catches, use loops that go to 10000 for no reason sprinkled in your code, hell, use angular/ember - you will not notice any performance issue.
If you are doing low level library, physics simulations, games, server-side etc then the never throwing try-catch block wouldn't normally matter at all but the problem is that V8 didn't support it in their optimizing compiler until version 6 of the engine, so the entire containing function that syntactically contains a try catch will not be optimized. You can easily work around this though, by creating a helper function like tryCatch
:
function tryCatch(fun) {
try {
return fun();
}
catch(e) {
tryCatch.errorObj.e = e;
return tryCatch.errorObj;
}
}
tryCatch.errorObj = {e: null};
var result = tryCatch(someFunctionThatCouldThrow);
if(result === tryCatch.errorObj) {
//The function threw
var e = result.e;
}
else {
//result is the returned value
}
After V8 version 6 (shipped with Node 8.3 and latest Chrome), the performance of code inside try-catch
is the same as that of normal code.
The original question asked about the cost of try/catch when an error was not thrown. There is definitely an impact when protecting a block of code with try/catch, but the impact of try/catch will vanish quickly as the code being protected becomes even slightly complex.
Consider this test: http://jsperf.com/try-catch-performance-jls/2
A simple increment runs at 356,800,000 iterations per second The same increment within a try/catch is 93,500,000 iterations per second. That's on overhead of 75% due to try/catch. BUT, a trivial function call runs at 112,200,000 iterations per second. 2 trivial function calls run at 61,300,000 iterations per second.
An un-exercised try in this test takes slightly more time than one trivial function call. That's hardly a speed penalty that matters except in the inner-most loop of something really intense like an FFT.
The case you want to avoid is the case where an exception is actually thrown. That is immensely slower, as shown in the above link.
Edit: Those numbers are for Chrome on my machine. In Firefox there is no significant difference between an unexercised try and no protection at all. There's essentially zero penalty to using try/catch if no exception is thrown.
The try-catch
block is said to be expensive. However if critical performance is not an issue, using it is not necessarily a concern.
The penalty IMO is:
- readability
- inappropriate in many cases
- ineffective when it comes to async programming
Readability: plumbing your code with plenty of try-catch is ugly and distracting
inappropriate: it's a bad idea to insert such block if your code is not subject to exception-crash. Insert it only if you expect a failure in your code. Take a look at the following topic: When to use try/catch blocks?
Async: the try-catch
block is synchronous and is not effective when it comes to async
programming. During an ajax
request you handle both the error
and success
events in dedicated callbacks. No need for try-catch
.
Hope this helps,
R.