Branch Prediction

JavaScript performance comparison

Revision 5 of this test case created

Info

Branch prediction comparison

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var i = -1,  arr= ["even","odd"];
};
</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
1. if-else
var result;
var n = ++i;
if(n % 2) {
  result = "odd";
} else {
  result = "even";
}
pending…
2. ternary
var n = ++i;
var result = (n % 2) ? "odd" : "even";
pending…
3. alt
var n = ++i;
(n % 2) ? (result= "odd") : (result = "even");
pending…
4. switch
var n=++i;
switch (n%2)
{ case true: result='odd'; break;
  default : result='even'
}
pending…
Array
var n = ++i;
result = arr[ n%2]
pending…
No branching
var result;
var n = ++i;
if(n % 2) {
  return result = "odd";
}
result = "even";
 
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. Here’s a list of current revisions for this page:

0 comments

Add a comment