Math.floor vs Math.round vs parseInt vs Bitwise

JavaScript performance comparison

Revision 120 of this test case created

Info

Rounding in all form possible

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
     var mathRound = function(val, exp) {
       if (exp) {
         return Math.round(val * Math.pow(10, exp)) / Math.pow(10, exp);
       } else {
         return Math.round(val);
       }
     };
};
</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.round
Math.round(89.938 / 293.3);
Math.round(83784 / 9289.2);
Math.round(7 / 60);
pending…
mathRound
mathRound(89.938 / 293.3);
mathRound(83784 / 9289.2);
mathRound(7 / 60);
pending…
parseFloat
parseFloat(89.938 / 293.3);
parseFloat(83784 / 9289.2);
parseFloat(7 / 60);
pending…
parseFloat - mathRound
parseFloat(mathRound(89.938 / 293.3));
parseFloat(mathRound(83784 / 9289.2));
parseFloat(mathRound(7 / 60));
pending…
toPrecision
(89.938 / 293.3).toPrecision(14);
(83784 / 9289.2).toPrecision(14);
(7 / 60).toPrecision(14);
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