Try/Catch Block Loop Performance Comparison (v16)

Revision 16 of this benchmark created on


Description

Measuring the performance implications of a try/catch block used with loops.

Setup

function foo() {
        for(var i = 0; i < 500; i++){
            var n = 1;
        }
    }
    function bar() {
        for(var i = 0; i < 500; i++){
            var n = 1;
        }
    }
    function zap() {
        for(var i = 0; i < 500; i++){
            var n = 1;
        }
        (0)();
    }
    function abc() {
        for(var i = 0; i < 500; i++){
            var n = 1;
        }
    }
    function def() {
        for(var i = 0; i < 500; i++){
            var n = 1;
        }
        (0)();
    }
    
    function tryWrap(closure, catchCB) {
       try {closure();}catch(e){if(catchCB){catchCB(e);}}
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Plain Loop
for(var i = 0; i < 500; i++){
    var n = 1;
}
ready
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
Loop in call
foo();
ready
Loop in call in try
try {
  bar();
} catch(e) {}
ready
Error in loop in call in try
try {
  zap();
} catch(e) {}
ready
Loop in wrapped closure
tryWrap(function() {
    for(var i = 0; i < 500; i++){
        var n = 1;
    }
});
ready
Error in loop in wrapped closure
tryWrap(function() {
    for(var i = 0; i < 500; i++){
        var n = 1;
    }
    (0)();
});
ready
Loop in static function wrapped
tryWrap(abc);
ready
Error in loop in static function, wrapped
tryWrap(def);
ready

Revisions

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