Math.floor vs Math.round vs parseInt vs Bitwise

JavaScript performance comparison

Revision 45 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;
};
</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
var x = Math.floor(a);
var y = Math.floor(x * b);
var z = Math.floor(y / c);
var w = Math.floor(z / a);
pending…
Math.round
var x = Math.round(a);
var y = Math.round(x * b);
var z = Math.round(y / c);
var w = Math.round(z / a);
pending…
parseInt
var x = parseInt(a);
var y = parseInt(x * b);
var z = parseInt(y / c);
var w = parseInt(z / a);
pending…
Bitwise |
var x = a | 0;
var y = (x * b) | 0;
var z = (y / c) | 0;
var w = (z / a) | 0;
pending…
Bitwise
var x = a >> 0;
var y = (x * b) >> 0;
var z = (y / c) >> 0;
var w = (z / a) >> 0;
pending…
Bit flipping
var x = ~~a;
var y = ~~(x * b);
var z = ~~(y / c);
var w = ~~(z / a);
pending…
zero-fill right shift
var x = a >>> 0;
var y = (x * b) >>> 0;
var z = (y / c) >>> 0;
var w = (z / a) >>> 0;
pending…
Alternate Bitwise |
var x = a | 0;
var y = x * b | 0;
var z = y / c | 0;
var w = z / a | 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