toFixed vs. Math.round when you need a string (v16)

Revision 16 of this benchmark created on


Setup

var test = [1.23456789, 2.3456, 3.4567, 4.5678, 5.6789];

Test runner

Ready to run.

Testing in
TestOps/sec
Math.round
for (var i = 0; i < test.length; i++) {
  '' + (Math.round(test[i] * 100) / 100);
}
ready
.toFixed
for (var i = 0; i < test.length; i++) {
  '' + test[i].toFixed(2);
}
ready
Binary 1
for (var i = 0; i < test.length; i++) {
  '' + (( test[i] * 100 >> 0 ) / 100);
}
ready
Binary 2
for (var i = 0; i < test.length; i++) {
  '' + ( ~~( test[i] * 100) / 100);
}
ready

Revisions

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