Try/Catch performance overhead (v22)

Revision 22 of this benchmark created by Oleksandr Kelepko on


Description

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.

Setup

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)}
    }

Teardown


    if(sum === 123456) console.log(sum);
  

Test runner

Ready to run.

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

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.