decimal rounding toFixed vs Math.round

JavaScript performance comparison

Revision 42 of this test case created by Stephen Irving

Preparation code


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

};
</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
Custom Function Mod 6
function fixToOnePlace(num) {
return (num = Math.round(num * 10) / 10) + (num % 1 === 0 ? '.0' : '');
}
fixToOnePlace(number);
pending…
Custom Function Mod 2
function fixToOnePlace(num) {
num = Math.round(num * 10) / 10;
return num + (num % 1 !== 0 ? '' : '.0');
}
fixToOnePlace(number)
pending…
Custom Function Mod 5 (4 with Arrow Function)
const fixToOnePlace = num => (num = Math.round(num * 10) / 10) + (0 === num % 1 ? '.0' : '');
fixToOnePlace(number);
pending…
Custom Function Mod 3
function fixToOnePlace(num) {
num = Math.round(num * 10) / 10;
return num + (!(num % 1) ? '.0' : '');
}
fixToOnePlace(number)
pending…
Custom Function Modified
function fixToOnePlace(num) {
num = Math.round(num * 10) / 10;
return num + (num % 1 ? '' : '.0');
}
fixToOnePlace(number);
pending…
toFixed string
number.toFixed(1)
pending…
Custom Function Mod 7 (6 with arrow function)
const fixToOnePlace = num => (num = Math.round(num * 10) / 10) + (num % 1 === 0 ? '.0' : '');
fixToOnePlace(number);
pending…
Custom Function
function fixToOnePlace(num) {
num = Math.round(num * 10) / 10;
return num + (num % 1 === 0 ? '.0' : '');
}
fixToOnePlace(number);
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 Mod 4
function fixToOnePlace(num) {
return (num = Math.round(num * 10) / 10) + (0 === 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