if/else verus if return;

JavaScript performance comparison

Test case created by drew

Info

While readability/maintainability is an argument not solved, is there a performance issue with if/else versus if return; implied else.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
   
    var ifRtnSimple = function( value ) {
        if( value ) {
                return value;
        }
        //implied else
        return value;
    }
   
    var ifElseSimple = function( value ) {
        if( value ) {
                return value;
        } else {
                return value;
        }
    }
   
    var ifElseVal = function( value ) {
        if( value ) {
                value = true;
        } else {
                value = false;
        }
        return value;
    }
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
ifRtnSimple true
ifRtnSimple(true);
pending…
ifRtnSimple false
ifRtnSimple(false);
pending…
ifElseSimple true
ifElseSimple(true);
pending…
ifElseSimple false
ifElseSimple(false);
pending…
ifElseVal true
ifElseVal(true);
pending…
ifElseVal false
ifElseVal(false);
pending…

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

Compare results of other browsers

0 comments

Add a comment