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

Revision 9 of this benchmark created on


Description

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

Preparation HTML

<script>
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){
    try {
        fn();
    } catch(e) {
        fail()
    }
}

function test(){
    var n = 1;  
}

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

function error(){
    return null;        
}
</script>

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
tryCatch(loop, error);
ready
tryCatch Function Inside Loop
for (var i = 0; i < 500; i++) {
  tryCatch(test, error);
}
ready
tryCatch Function Outside Loop Native
try {
  loop()
} catch (e) {}
ready

Revisions

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