Looking at perf for toFixed identified the multiplication (and its derivatives) as being slow. However for base 10, this can be done much faster by just adjusting the exponent. toString is trickier, but this improves the performance slightly.
Updated benchmark results for the benchmark seen in the issue:
BigDecimal#toFixed x 2,170,232 ops/sec ±2.82% (85 runs sampled)
Decimal#toFixed x 3,081,856 ops/sec ±0.60% (91 runs sampled)
Fastest is Decimal#toFixed
BigDecimal#toString x 659,071 ops/sec ±0.37% (91 runs sampled)
Decimal#toString x 12,362,590 ops/sec ±1.36% (83 runs sampled)
Fastest is Decimal#toString
So 2x the perf for toFixed, and a ~10% bump for toString (as measured relative to decimal.js).
Note that a lot of the drop in toFixed is from doing the sum. If I just add them together, it's almost on par with decimal.js. However I'm concerned about fractionDigits not being a whole number, and other assumptions being thwarted about the type of the exponent.
Looking at perf for toFixed identified the multiplication (and its derivatives) as being slow. However for base 10, this can be done much faster by just adjusting the exponent. toString is trickier, but this improves the performance slightly.
See: https://github.com/Yaffle/BigDecimal/issues/10
Updated benchmark results for the benchmark seen in the issue:
So 2x the perf for toFixed, and a ~10% bump for toString (as measured relative to decimal.js).
Note that a lot of the drop in toFixed is from doing the
sum
. If I just add them together, it's almost on par with decimal.js. However I'm concerned about fractionDigits not being a whole number, and other assumptions being thwarted about the type of the exponent.