Try Catch Closures

Benchmark created by Miller Medeiros on


Description

Test to check if different placements of try/catch blocks affect the performance of surrounding code.

Preparation HTML

<script>
  var errors = [];
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
same fn
var test1;

//create a closure on purpose
(function(){

    test1 = {
        doWork : function(a, b){
            var val;
            try {
                val = a + b;
            } catch(e) {
                errors.push(e); //make sure catch isn't just skiped by JIT
            }

            // do something expensive
            var n = 5000;
            var result = '';
            while(n--){
                result += val;
            }
            return result;
        }
    };

}());

test1.doWork(2, 2);
ready
other fn
var test2;

//create a closure on purpose
(function(){


    test2 = {
        doWork : function(a, b){
            var val = safeSum(a, b);
            
            // do something expensive
            var n = 5000;
            var result = '';
            while(n--){
                result += val;
            }
            return result;
        }
    };
    
    //try catch on a different block to test if it still affects performance of
    //the loop
    function safeSum(a, b){
        var result;
        try {
            result = a + b;
        } catch(e) {
            errors.push(e); //make sure catch isn't just skiped by JIT
        }
        return result;
    }

}());


test2.doWork(2, 2);
 
ready
inner closure
var test3;

//create a closure on purpose
(function(){

    var safeSum;

    test3 = {
        doWork : function(a, b){
            var val = safeSum(a, b);
            
            // do something expensive
            var n = 5000;
            var result = '';
            while(n--){
                result += val;
            }
            return result;
        }
    };
    
    //another closure to see if it changes performance
    (function(){
        safeSum = function(a, b){
            var result;
            try {
                result = a + b;
            } catch(e) {
                errors.push(e); //make sure catch isn't just skiped by JIT
            }
            return result;
        };

    }());
    
}());


test3.doWork(2, 2);
ready
outer closure
var test4;
var safeSum4;

//create a closure on purpose
(function(){

    test4 = {
        doWork : function(a, b){
            var val = safeSum4(a, b);
            
            // do something expensive
            var n = 5000;
            var result = '';
            while(n--){
                result += val;
            }
            return result;
        }
    };
        
}());


//another closure to see if it changes performance
(function(){
    safeSum4 = function(a, b){
        var result;
        try {
            result = a + b;
        } catch(e) {
            errors.push(e); //make sure catch isn't just skiped by JIT
        }
        return result;
    };
}());


test4.doWork(2, 2);
ready
wrap all
var test5;

//create a closure on purpose
(function(){

    test5 = {
        doWork : function(a, b){
            var val;
            var result = '';
            var n = 5000;

            try {
                val = a + b;
                // do something expensive
                while(n--){
                    result += val;
                }
            } catch(e) {
                errors.push(e); //make sure catch isn't just skiped by JIT
            }

            return result;
        }
    };

}());

test5.doWork(2, 2);
ready

Revisions

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

  • Revision 1: published by Miller Medeiros on