Try/Catch Block Loop Performance Comparison (v7)

Revision 7 of this benchmark created on


Description

Measuring the performance implications of a try/catch block used with loops.

Setup

function trycatch(t, c) {
      try {
        t()
      } catch (e) {
        if (c) c(e);
      }
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Plain Loop
for (var i = 0; i < 500; i++) {
  var n = 1;
}
ready
Try/Catch Block Outside Loop
try {
  for (var i = 0; i < 500; i++) {
    var n = 1;
  }
} catch (e) {}
ready
Try/Catch Block Inside Loop
for (var i = 0; i < 500; i++) {
  try {
    var n = 1;
  } catch (e) {}
}
ready
Try/Catch Loop in Function
function loop() {
  for (var i = 0; i < 500; i++) {
    var n = 1;
  }
}

try {
  loop();
} catch (e) {}
ready
trycatch function
var n;
trycatch(function() {
  for (var i = 0; i < 500; i++) {
    n = 1;
  }
});
ready
trycatch with saved function
var n;

function loop() {
  for (var i = 0; i < 500; i++) {
    n = 1;
  }
}
trycatch(loop);
ready

Revisions

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