multi try catch VS single

JavaScript performance comparison

Revision 2 of this test case created

Info

check overhead of using try catch on every operation VS on a single wrapping operation and with errors and without

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var testAmount = 100;
   
    function runTest1(index) {
      index = index || 0;
      var i;
      try {
        for (i = index; i < testAmount; ++i) {
          window.x = random();
          throw new Error('test error');
        }
      } catch (e) {
        var y = e.stack;
        runOnError1(i + 1);
      }
    }
   
    function runOnError1(index) {
      runTest1(index);
    }
   
    function runTest2() {
      for (var i = 0; i < testAmount; ++i) {
        try {
          window.x = random();
          throw new Error('test error');
        } catch (e) {
          var y = e.stack;
        }
      }
    }
};
</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
Multi try catch - no fail
function runTest() {
  for (var i = 0; i < testAmount; ++i) {
    try {
      window.x = random();
    } catch (e) {
      var y = e.stack;
    }
  }
}
runTest()
pending…
Single try catch - no fail
function runTest() {
  try {
    for (var i = 0; i < testAmount; ++i) {
      window.x = random();
    }
  } catch (e) {
    var y = e.stack;
  }
}
runTest()
pending…
Multi try catch - fail
function runTest() {
  for (var i = 0; i < testAmount; ++i) {
    try {
      window.x = random();
      throw new Error('test error');
    } catch (e) {
      var y = e.stack;
    }
  }
}
runTest()
pending…
Single try catch - fail
function runTest(index) {
  index = index || 0;
  var i;
  try {
    for (i = index; i < testAmount; ++i) {
      window.x = random();
      throw new Error('test error');
    }
  } catch (e) {
    var y = e.stack;
    runOnError(i + 1);
  }
}

function runOnError(index) {
  runTest(index);
}
runTest()
pending…
Single try catch - fail - same func eval
runTest1();
pending…
Multi try catch - fail - same func eval
runTest2();
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