if/else verus if return;
JavaScript performance comparison
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.
| Test | Ops/sec | |
|---|---|---|
ifRtnSimple true |
|
pending… |
ifRtnSimple false |
|
pending… |
ifElseSimple true |
|
pending… |
ifElseSimple false |
|
pending… |
ifElseVal true |
|
pending… |
ifElseVal false |
|
pending… |
You can edit these tests or add even more tests to this page by appending /edit to the URL.
0 comments