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)
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    Simple rounding
    Rounding with epsilon and string conversion
    Rounding with "if" statement
    Rounding by toLocaleString

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 5 years ago)
Mozilla/5.0 (Android 4.4.2; Tablet; rv:68.0) Gecko/68.0 Firefox/68.0
Android 4 on Android 4.4.2
View result in a separate tab
Test name Executions per second
Simple rounding 10991604.0 Ops/sec
Rounding with epsilon and string conversion 184544.5 Ops/sec
Rounding with "if" statement 5203109.5 Ops/sec
Rounding by toLocaleString 954.4 Ops/sec