Math.floor divide 2 vs bit shift
JavaScript performance comparison
Info
added *0.5 test renamed tests
Preparation code
<script>
function caseDiv() {
var sum = 0;
for (var i = 0; i < 100; i++) {
sum += Math.floor(i / 2);
}
return sum;
}
function caseShift() {
var sum = 0;
for (var i = 0; i < 100; i++) {
sum += (i >> 1);
}
return sum;
}
function caseMul() {
var sum = 0;
for (var i = 0; i < 100; i++) {
sum += Math.floor(i * 0.5);
}
return sum;
}
function caseMul2() {
var sum = 0;
for (var i = 0; i < 100; i++) {
sum += (i * 0.5)0;
}
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.
Test  Ops/sec  

Math.floor /2 

pending… 
Bit shift 

pending… 
Math.floor *.5 

pending… 
0 

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:
 Revision 1: published by Ed Sanders
 Revision 2: published
 Revision 3: published by tomByrer
 Revision 7: published
 Revision 8: published
 Revision 9: published
 Revision 10: published
0 comments