Run details:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36
Chrome 117
Mac OS X 10.15.7
Desktop
one year ago
Test name Executions per second
myFloat.toFixed(2) 1442765.0 Ops/sec
myFloat.toPrecision(2) 1664526.0 Ops/sec
Math.round(myFloat*100)/100 i.e. 2 places 1388430.2 Ops/sec
Bitwise: signed round to 2 places 1359184.5 Ops/sec
Bitwise: UNsigned round to 2 places 1999135.2 Ops/sec
myFloat.toFixed(0) i.e. round to int 1493618.0 Ops/sec
Math.round(myFloat) 1804318.8 Ops/sec
Bitwise: signed round to nearest int 3153633.2 Ops/sec
Floor (round down only to int) 1700823.5 Ops/sec
Trunc (round towards zero) 1646082.4 Ops/sec
Bitwise: signed round to zero (trunc) 3202424.2 Ops/sec
Script Preparation code:
Tests:
  • myFloat.toFixed(2)

  • myFloat.toPrecision(2)

  • Math.round(myFloat*100.0)/100.0

  • Bitwise: signed round ± to 2 places

  • Bitwise: UNsigned round ± to 2 places

  • myFloat.toFixed(0)

  • Math.round()

  • Bitwise: signed round ± to int

  • Bitwise: UNsigned round ± to int

  • Math.floor() round down

  • Math.trunc() round toward zero

  • Bitwise: round toward zero