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

Revision 149 of this benchmark created by Leo Dutra on


Description

Rounding in all form possible

Setup

var math_round = function( n ) {
      return n < 2147483646 && n > - 2147483647 ? ~~(n + 0.5) : Math.round(n);
    }
    
    var fast_math_round = function( n ) {
      return (n & 2147483647) === n || (n & -2147483648) === n ? ~~(n + 0.5) : Math.round(n);
    }
    
    var fastest_math_round = function( n ) {
      return ~~(n + 0.5);
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Math.floor
Math.floor(89.938 / 293.3);
Math.floor(83784 / 9289.2);
Math.floor(7 / 60);
ready
Math.round
Math.round(89.938 / 293.3);
Math.round(83784 / 9289.2);
Math.round(7 / 60);
ready
parseInt
parseInt(89.938 / 293.3);
parseInt(83784 / 9289.2);
parseInt(7 / 60);
ready
Bitwise |
89.938 / 293.3 | 0;
83784 / 9289.2 | 0;
7 / 60 | 0;
ready
Bitwise >>
89.938 / 293.3 >> 0;
83784 / 9289.2 >> 0;
7 / 60 >> 0;
ready
Bitwise ~~
~~(89.938 / 293.3);
~~(83784 / 9289.2);
~~(7 / 60);
ready
Bitwise <<
89.938 / 293.3 << 0;
83784 / 9289.2 << 0;
7 / 60 << 0;
ready
math_round
math_round(89.938 / 293.3);
math_round(83784 / 9289.2);
math_round(7);
ready
fast_math_round
fast_math_round(89.938 / 293.3);
fast_math_round(83784 / 9289.2);
fast_math_round(7);
ready
fastest_math_round
fastest_math_round(89.938 / 293.3);
fastest_math_round(83784 / 9289.2);
fastest_math_round(7);
ready

Revisions

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