Native Try/Catch vs. Custom tryCatch Function in a Loop (v5)

Revision 5 of this benchmark created by Patrick Fischer on


Description

Investigating a means of replacing the try catch block with a custom solution for performance intensive operations in a loop.

Setup

var callback = null;
    
    window.onerror = function(msg, file, line) {
      var error = new Error(msg, file, line);
      if (callback) {
        callback(error);
        callback = null;
      }
      return true;
    };
    
    function tryCatch(fn, fail) {
      callback = fail;
      fn();
      callback = null;
    }

Test runner

Ready to run.

Testing in
TestOps/sec
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
tryCatch Function Outside Loop
function loop() {
  for (var i = 0; i < 500; i++) {
    var n = 1;
  }
}

function error() {
  return null;
}

tryCatch(loop, error);
ready
tryCatch Function Inside Loop
function test() {
  var n = 1;
}

function error() {
  return null;
}

for (var i = 0; i < 500; i++) {
  tryCatch(test, error);
}
ready
Try/Catch Block Outside Loop (Optimized)
function loop() {
  for (var i = 0; i < 500; i++) {
    var n = 1;
  }
}

try {
  loop();
} catch (e) {}
ready

Revisions

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