JavaScript / field notes

An independent proposal for decimal.js 10.6.0

Two small changes
to decimal division.

Reuse a quotient you already have.
Fill a fraction once its remainder stops changing.

Two separate patches, with exact tests and measurements for each step. The same digits, signs, exponents and rounding.

Small enough to review separately

Two focused optimizations.

The second patch builds on the first. Generic reciprocal growth is outside this proposal.

PATCH 01

Calculate the remainder from the quotient.

Division already produced the quotient limb. Subtract its product with the divisor to get the exact remainder.

Read patch 01, including tests
Before
q = t / divisor | 0;
r = t % divisor | 0;

After
q = t / divisor | 0;
r = t - q * divisor;

The products remain exact integers in JavaScript Number.

PATCH 02

Fill a proven repeating fraction.

For a long fractional tail, check whether one more step returns the same remainder. If it does, every remaining limb is identical.

Read patch 02, including tests
remainder 2remainder 2

Terminating fractions still stop early. Other fractions continue through the ordinary division loop. The nonzero remainder is retained for rounding.

Applies to one-limb divisors and sufficiently long zero-padded tails. All output limbs are still written.

Measured one change at a time

Where the time goes.

Apple M1 Max, Node 24.22.1 / arm64. Two candidate runs and an identical-code control. Ratios above 1 mean faster.

10,000 significant decimal digits. Ranges show the two runs’ medians of paired-round ratios. Patch 02 is measured on top of patch 01.
WorkloadPatch 01 vs stockAdditional gain from 02
Repeating fraction · 2 / 33.66×4.24–4.58×
Generic fraction · 3 / 73.26–3.32×1.00–1.01×
Full-length input / 973.30–3.39×1.00–1.01×
64 changing input pairs1.78–1.81×1.13–1.15×
2 / 3, including text conversion2.77–2.78×2.03×
The controls matter. The largest observed slowdowns were 5.53% for patch 01 and 5.05% for patch 02, in individual control cases. Identical-code A/A ratios varied by about 2.7%. See every case and both runs.
Inspect timings for a case
Stock80.872 µs
After patch 0122.144 µs
After patch 025.053 µs

Bars show median operation times from run 1. The table above uses paired ratios from both runs. Input creation and exact checks are outside the timer.

Review the change. Reproduce the evidence.

A compact contribution.

128,570 exact-state, independent rounding-oracle and module-parity comparisons passed. All 60 upstream test modules passed for stock and both patched stages.

The package contains the two patches, their regression tests, a benchmark runner, raw results and pinned upstream source. Earlier experiments stay outside it.

Package SHA-256 · Complete results · Raw evidence

Run from the extracted package

python3 reproduce.py --output /tmp/decimal-review

Requires Node.js, Python 3.10+ and patch. No package installation or network access. Use a new output directory.

48 cases, 11 shuffled paired rounds and batches of at least 3 ms. Correctness checked before and after timing. These are local microbenchmarks, not application-wide speedups.