Closed imirkin closed 1 year ago
@imirkin what browser are you using?
I've just been doing the microbenchmarks on Node v18.16, because it's easier. The ultimate target is Chrome, I've been hoping that the results carry over.
Looks like the "+" isn't a big perf difference. ZERO_CHAR is a big jump:
Without ZERO_CHAR, from a few runs:
BigDecimal#toFixed x 618,175 ops/sec ±0.63% (95 runs sampled)
BigDecimal#toFixed x 623,340 ops/sec ±0.96% (91 runs sampled)
BigDecimal#toFixed x 609,253 ops/sec ±0.50% (94 runs sampled)
BigDecimal#toFixed x 629,028 ops/sec ±0.43% (94 runs sampled)
With ZERO_CHAR (and the +):
BigDecimal#toFixed x 648,698 ops/sec ±0.59% (97 runs sampled)
BigDecimal#toFixed x 643,930 ops/sec ±0.77% (94 runs sampled)
BigDecimal#toFixed x 638,103 ops/sec ±0.37% (96 runs sampled)
BigDecimal#toFixed x 649,597 ops/sec ±0.43% (93 runs sampled)
I've tried my best to make the setup stable for benchmarking, but apparently still not -- enabled performance governor, forced a single cpu for node running. Even though the results are slightly unstable, they do seem to indicate non-overlapping ranges.
Perhaps the node v8 is older than the Chrome one, and some of this is for naught, but I'm not sure how to get proper benchmarks for this stuff going in Chrome.
@imirkin could you test on the latest code from the master?
Did a couple runs on latest master (e4c3f3ee588ad4244664af353972c6a15bd5cef2):
BigDecimal.BigDecimal x 1,012,460 ops/sec ±1.43% (91 runs sampled)
Decimal#constructor x 438,019 ops/sec ±0.50% (97 runs sampled)
Big#constructor x 677,697 ops/sec ±0.95% (91 runs sampled)
Fastest is BigDecimal.BigDecimal
BigDecimal#toFixed x 843,683 ops/sec ±0.82% (96 runs sampled)
Decimal#toFixed x 626,912 ops/sec ±1.01% (95 runs sampled)
Big#toFixed x 656,388 ops/sec ±1.74% (94 runs sampled)
Fastest is BigDecimal#toFixed
BigDecimal#toString x 2,664,587 ops/sec ±0.60% (94 runs sampled)
Decimal#toString x 2,574,290 ops/sec ±0.52% (95 runs sampled)
Big#toString x 1,353,320 ops/sec ±0.38% (95 runs sampled)
Fastest is BigDecimal#toString
Looks like success!
BTW, you could still remove one layer of Math.max() and allow "zeros" to be negative. IME that's worth some improvement, but ... up to you.
This reduces the number of operations in
bigDecimalToPlainString
. Every little bit counts. With this, toString / toFixed are on par with big.js for a few sample values, and decimal.js's toFixed. Somehow decimal.js toString is still WAY faster.The tests seem to pass with this, but I didn't do a ton of checking to make sure it was OK.
For the values