Math.floor vs Math.round vs parseInt vs Bitwise (v51)

Revision 51 of this benchmark created by Jon-Carlos Rivera on


Description

Rounding in all form possible

Setup

var a = Math.random() * 1000;
    var b = Math.random() * 1000000;
    var c = Math.random() * 1000000000;
    var d = Math.random() * 10;

Test runner

Ready to run.

Testing in
TestOps/sec
Math.floor
a = Math.floor(a / d);
a *= Math.PI;
b = Math.floor(b / a);
b *= Math.PI;
c = Math.floor(c / b);
c *= Math.PI;
ready
Math.round
a = Math.round(a / d);
a *= Math.PI;
b = Math.round(b / a);
b *= Math.PI;
c = Math.round(c / b);
c *= Math.PI;
ready
parseInt
a = parseInt(a / d);
a *= Math.PI;
b = parseInt(b / a);
b *= Math.PI;
c = parseInt(c / b);
c *= Math.PI;
ready
Bitwise |
a = (a / d) | 0;
a *= Math.PI;
b = (b / a) | 0;
b *= Math.PI;
c = (c / b) | 0;
c *= Math.PI;
ready
Bitwise
a = (a / d) >> 0;
a *= Math.PI;
b = (b / a) >> 0;
b *= Math.PI;
c = (c / b) >> 0;
c *= Math.PI;
ready
Bit flipping
a = ~~(a / d);
a *= Math.PI;
b = ~~(b / a);
b *= Math.PI;
c = ~~(c / b);
c *= Math.PI;
ready
zero-fill right shift
a = (a / d) >>> 0;
a *= Math.PI;
b = (b / a) >>> 0;
b *= Math.PI;
c = (c / b) >>> 0;
c *= Math.PI;
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.