Round number with X decimals (toFixed VS Math.round)

Benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
toFixed
const formatNumber = (x, fractionDigits) => {
  return Number(x.toFixed(fractionDigits ?? 3).replace(/\.?0*$/, ''))
}
ready
Math.round
const formatNumber = (x, fractionDigits) => {
  const powed = Math.pow(10, fractionDigits ?? 2)
  return Math.round(Number(String(x).replace(/,/gi, '.')) * powed) / powed
}
ready

Revisions

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