decimal rounding toFixed vs Math.round

JavaScript performance comparison

Revision 34 of this test case created by Stephen Irving

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var number = Math.random();

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Math.round string
'' + Math.round(number * 10) / 10
pending…
toFixed
Number((number).toFixed(1));
pending…
toFixed string
number.toFixed(1)
pending…
Custom Function Out of Scope
const fixedToOne = function (num) {
num = Math.round(num * 10) / 10;
return num + (num % 1 === 0 ? '.0' : '');
}
function someOtherFunction() { return fixedToOne(number); }
someOtherFunction();
pending…
Custom Function
function fixToOnePlace(num) {
  num = Math.round(num * 10) / 10;
  return num + (num % 1 === 0 ? '.0' : '');
}
fixToOnePlace(number);
pending…
Math.round
Math.round(number * 10) / 10
pending…
Custom Function Modified
function fixToOnePlace(num) {
  num = Math.round(num * 10) / 10;
  return num + (num % 1 ? '' : '.0');
}
fixToOnePlace(number);
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.

0 Comments