Try/Catch performance overhead

JavaScript performance comparison

Revision 22 of this test case created by Oleksandr Kelepko


Using try/catch inside of functions that allocate lots of variables introduces significant overhead. See this discussion related to node.js!topic/nodejs-dev/E-Re9KDDo5w

Interestingly, other js engines don't have this overhead. All test runs have pretty uniform performance. But they are also significantly slower than the v8 control case.

Preparation code

Benchmark.prototype.setup = function() {
    var n = Math.random();
    var sum = 0;
    function compute(x) {
      return Math.cos(x);
    function withoutTry(x) {
      return compute(x);
    function withTry(x) {
        return compute(x);
      } catch(e){console.log(e)}

Benchmark.prototype.teardown = function() {
    if(sum === 123456) console.log(sum);

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
control - no try/catch
sum += withoutTry(n);
try/catch inside function
sum += withTry(n);

Compare results of other browsers


You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:


Comment form temporarily disabled.

Add a comment