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

Revision 124 of this benchmark created on


Description

Rounding in all form possible

Setup

var mathRound = function(val, exp) {
       if (exp) {
         return Math.round(val * Math.pow(10, exp)) / Math.pow(10, exp);
       } else {
         return Math.round(val);
       }
     };

Test runner

Ready to run.

Testing in
TestOps/sec
mathRound
mathRound((89.938 / 293.3), 14);
mathRound((83784 / 9289.2, 14));
mathRound((7 / 60, 14));
ready
toPrecision
(89.938 / 293.3).toPrecision(14);
(83784 / 9289.2).toPrecision(14);
(7 / 60).toPrecision(14);
ready
staight Forward
Math.round((89.938 / 293.3) * Math.pow(10, 14)) / Math.pow(10, 14);
Math.round((83784 / 9289.2) * Math.pow(10, 14)) / Math.pow(10, 14);
Math.round((7 / 60) * Math.pow(10, 14)) / Math.pow(10, 14);
ready
parseFloat - round
parseFloat(Math.round((89.938 / 293.3) * Math.pow(10, 14)) / Math.pow(10, 14));
parseFloat(Math.round((83784 / 9289.2) * Math.pow(10, 14)) / Math.pow(10, 14));
parseFloat(Math.round((7 / 60) * Math.pow(10, 14)) / Math.pow(10, 14));
ready

Revisions

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