Math.floor vs Math.round vs parseInt vs Bitwise

JavaScript performance comparison

Revision 21 of this test case created by Sam

Info

Rounding in all possible forms

Preparation code

var a;

Preparation code output

var a;

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(89.938 / 293.3);
a = Math.floor(83784 / 9289.2);
a = Math.floor(7 / 60);
pending…
Math.round
a = Math.round(89.938 / 293.3);
a = Math.round(83784 / 9289.2);
a = Math.round(7 / 60);
pending…
parseInt
a = parseInt(89.938 / 293.3);
a = parseInt(83784 / 9289.2);
a = parseInt(7 / 60);
pending…
parseInt( ,10)
a = parseInt(89.938 / 293.3, 10);
a = parseInt(83784 / 9289.2, 10);
a = parseInt(7 / 60, 10);
pending…
Bitwise |
a = (89.938 / 293.3) | 0;
a = (83784 / 9289.2) | 0;
a = (7 / 60) | 0;
pending…
Bitwise >>
a = (89.938 / 293.3) >> 0;
a = (83784 / 9289.2) >> 0;
a = (7 / 60) >> 0;
pending…
Bitwise ~~
a = ~~(89.938 / 293.3);
a = ~~(83784 / 9289.2);
a = ~~(7 / 60);
pending…
Bitwise <<
a = (89.938 / 293.3) << 0;
a = (83784 / 9289.2) << 0;
a = (7 / 60) << 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:

1 comment

Solenoid commented :

FYI: bitwise rounding doesn't work correctly with negative numbers: -2.1|0 === 2 whereas Math.floor(-2.1) === -3.

Add a comment