Math.floor vs Math.round vs parseInt vs Bitwise

JavaScript performance comparison

Revision 52 of this test case created by Jon-Carlos Rivera

Info

Rounding in all form possible

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var a = Math.random() * 1000;
    var b = Math.random() * 1000000;
    var c = Math.random() * 1000000000;
    var d = Math.random() * 10;
   
};
</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
a = Math.floor(a / d);
a *= Math.PI;
a = Math.floor(b / a);
a *= Math.PI;
a = Math.floor(c / b);
a *= Math.PI;
pending…
Math.round
a = Math.round(a / d);
a *= Math.PI;
a = Math.round(b / a);
a *= Math.PI;
a = Math.round(c / b);
a *= Math.PI;
pending…
parseInt
a = parseInt(a / d);
a *= Math.PI;
a = parseInt(b / a);
a *= Math.PI;
a = parseInt(c / b);
a *= Math.PI;
pending…
Bitwise |
a = (a / d) | 0;
a *= Math.PI;
a = (b / a) | 0;
a *= Math.PI;
a = (c / b) | 0;
a *= Math.PI;
pending…
Bitwise
a = (a / d) >> 0;
a *= Math.PI;
a = (b / a) >> 0;
a *= Math.PI;
a = (c / b) >> 0;
a *= Math.PI;
pending…
Bit flipping
a = ~~(a / d);
a *= Math.PI;
a = ~~(b / a);
a *= Math.PI;
a = ~~(c / b);
a *= Math.PI;
pending…
zero-fill right shift
a = (a / d) >>> 0;
a *= Math.PI;
a = (b / a) >>> 0;
a *= Math.PI;
a = (c / b) >>> 0;
a *= Math.PI;
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