Try/Catch performance overhead

JavaScript performance comparison

Revision 22 of this test case created by Oleksandr Kelepko

Info

Using try/catch inside of functions that allocate lots of variables introduces significant overhead. See this discussion related to node.js https://groups.google.com/forum/#!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

 
<script>
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) {
      try{  
        return compute(x);
      } catch(e){console.log(e)}
    }
};

Benchmark.prototype.teardown = function() {
    if(sum === 123456) console.log(sum);
};
</script>

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);
 
pending…
try/catch inside function
sum += withTry(n);
pending…

Compare results of other browsers

Revisions

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:

0 comments

Add a comment