Branch Prediction

JavaScript performance comparison

Revision 2 of this test case created by doug65536

Info

Branch prediction comparison

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var i = 1000;
};
</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
if-else modulus
var result;
for (var n = 0; n < i; ++n) {
  if (n % 2) {
    result = "odd";
  } else {
    result = "even";
  }
}
 
pending…
ternary value return assign
var result;
for (var n = 0; n < i; ++n) {
  result = (n % 2) ? "odd" : "even";
}
 
pending…
ternary value assign return
var result;
for (var n = 0; n < i; ++n) {
  (n % 2) ? (result = "odd") : (result = "even");
}
 
pending…
bitwise if
var result;
for (var n = 0; n < i; ++n) {
  if (n & 1) {
    result = "odd";
  } else {
    result = "even";
  }
}
 
pending…
bitwise ternary value return assign
var result;
for (var n = 0; n < i; ++n) {
  result = (n & 1) ? "odd" : "even";
}
 
pending…
ternary value assign return
var result;
for (var n = 0; n < i; ++n) {
  (n & 1) ? (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