Math.floor divide 2 vs bit shift

JavaScript performance comparison

Revision 2 of this test case created

Preparation code

<script>

function case1() {
  var sum = 0;
  for (var i = 0; i < 100; i++) {
    sum += Math.floor(i / 2);
  }
  return sum;
}

function case2() {
  var sum = 0;
  for (var i = 0; i < 100; i++) {
    sum += (i >> 1);
  }
  return sum;
}


</script>
<script>
Benchmark.prototype.setup = function() {
    sum = -1;
};

Benchmark.prototype.teardown = function() {
    if (sum != 2450) throw "error";
};
</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
Math.floor divide two
sum &= case1();
pending…
Bit shift
sum &= case2();
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