Math.floor vs Math.round vs parseInt vs Bitwise

JavaScript performance comparison

Revision 53 of this test case created

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
 Math.floor(a / d);
Math.floor(b / a);
Math.floor(c / b);
pending…
Math.round
Math.round(a / d); Math.round(b / a);
Math.round(c / b);
pending…
parseInt
 parseInt(a / d);
 parseInt(b / a);
 parseInt(c / b);
pending…
Bitwise |
(a / d) | 0;
(b / a) | 0;
 (c / b) | 0;
pending…
Bitwise
 (a / d) >> 0;
 (b / a) >> 0;
 (c / b) >> 0;
pending…
Bit flipping
 ~~(a / d);
 ~~(b / a);
 ~~(c / b);
pending…
zero-fill right shift
(a / d) >>> 0;
(b / a) >>> 0;
(c / b) >>> 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:

0 comments

Add a comment