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

JavaScript performance comparison

Test case created by Ryan Morr

Preparation code

<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;	
}
</script>
    

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Try/Catch Block Outside Loop
try{
    for(var i = 0; i < 500; i++){
        var n = 1;
    }
}catch(e){}
pending…
Try/Catch Block Inside Loop
for(var i = 0; i < 500; i++){
    try{
        var n = 1;
    }catch(e){}
}
pending…
tryCatch Function Outside Loop
tryCatch(loop, error);
pending…
tryCatch Function Inside Loop
for(var i = 0; i < 500; i++){
    tryCatch(test, error);
}
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.

0 Comments