Run details:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36
Chrome 74
Windows
Desktop
5 years ago
Test name Executions per second
Simple rounding 4198704.5 Ops/sec
Rounding with epsilon and string conversion 1579440.2 Ops/sec
Rounding with "if" statemen 1489148.9 Ops/sec
Script Preparation code:
x
 
function round2(num, places) {
  return +(Math.round(num + "e+" + places)  + "e-" + places);
}
function round3(num, places) {
  if (places === 2) { 
    return Math.round( Math.round( num * 1000 ) / 10 ) / 100
  } else {
    const multiplier = Math.pow(10, places)
    return Math.round(num * multiplier) / multiplier
  }
}
function round4(num, places) {
  +(num.toLocaleString(
    'en', 
    { maximumFractionDigits: places, useGrouping: false }
  ))
}
Tests:
  • Simple rounding

     
    Math.round(12.345 * 100) / 100
  • Rounding with epsilon and string conversion

     
    round2(12.345, 2)
  • Rounding with "if" statement

     
    round3(12.345, 2)
  • Rounding by toLocaleString

     
    round4(12.345, 2)