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

Revision 11 of this benchmark created by Jérôme 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){
    callback = fail;
    fn();
    callback = null;
}

function test(){
    var n = 1;  
}

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

function error(){
    return null;        
}
function trycatch2(f, g){try {f()} catch (e) {g(e)}}
</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
tryCatch2 Function Outside Loop
trycatch2(loop, error);
ready
tryCatch2 Function Inside Loop
for(var i = 0; i < 500; i++){
    trycatch2(test, error);
}
ready

Revisions

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