Try/Catch Block vs. Custom tryCatch Function

JavaScript performance comparison

Revision 7 of this 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 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
try{
    var n = 1;
}catch(e){}
pending…
tryCatch Function (Anonymous)
tryCatch(function(){
    var n = 1;    
}, function(error){
    return null;
});
pending…
tryCatch Function (Named)
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.

2 Comments

lpsantil commented :

Native try catch is fastest for me on Chrome 30.

cristobal commented :

Native is fastest on Safari