A $100,000 revised contract has $49,999.99 of installed work and $7,500.00 of stored materials. This payment application comes to $11,624.99. Add one cent of installed work and the application comes to $14,312.50. The extra $2,687.51 includes one cent of new work and $2,687.50 of released retainage.
Decide what counts toward 50%
The fixture starts with a $95,000 contract. An approved $7,500 addition and an approved $2,500 deduction bring it to $100,000. A pending $1,000 change stays outside the revised amount. The two SOV lines already include their shares of the approved changes, and prior certificates total $40,500.
This example uses a cumulative step-down policy. It holds 10% on installed work and 5% on stored materials until installed work reaches half the revised contract. At that point the rates drop to 5% and 2.5% on their respective cumulative amounts.
| Calculation | One cent below | At 50% |
|---|---|---|
| Installed work to date | $49,999.99 | $50,000.00 |
| Stored materials at cutoff | $7,500.00 | $7,500.00 |
| Gross billed to date | $57,499.99 | $57,500.00 |
| Work retainage | $5,000.00 | $2,500.00 |
| Stored retainage | $375.00 | $187.50 |
| Earned less retainage | $52,124.99 | $54,812.50 |
| Less prior certificates | $40,500.00 | $40,500.00 |
| Current payment due | $11,624.99 | $14,312.50 |
A trigger based on gross billed to date would fire early because it includes the stored materials. The engine compares twice the installed cents with the revised contract cents. That keeps the threshold independent of the percentage rounding in a dashboard.
Benchmark the whole application
Run this block from the library checkout with Node.js 20 or later. It verifies the two threshold cases, the same application with base rates throughout, and a line with a value beyond JavaScript's safe integer range. The timed loop includes the complete calculation and input validation. MIT source repository
node --input-type=module <<'JS'
import assert from 'node:assert/strict';
import { performance } from 'node:perf_hooks';
import { calculatePayApplication } from './src/index.js';
const base = {
originalContractCents: 9_500_000,
priorCertificatesCents: 4_050_000,
changeOrders: [
{ id: 'CO-1', status: 'approved', amountCents: 750_000 },
{ id: 'CO-2', status: 'approved', amountCents: -250_000 },
{ id: 'CO-3', status: 'pending', amountCents: 100_000 },
],
retainage: {
workBps: 1_000, storedBps: 500,
stepDown: { workBps: 500, storedBps: 250 },
},
lines: [
{ id: 'Shell', scheduledCents: 6_000_000, previousWorkCents: 3_000_000,
currentWorkCents: 250_000, storedMaterialsCents: 500_000 },
{ id: 'Fit-out', scheduledCents: 4_000_000, previousWorkCents: 1_500_000,
currentWorkCents: 250_000, storedMaterialsCents: 250_000 },
],
};
const cases = [
[{ ...base, lines: [{ ...base.lines[0], currentWorkCents: 249_999 }, base.lines[1]] }, 1_162_499n, false],
[base, 1_431_250n, true],
[{ ...base, retainage: { workBps: 1_000, storedBps: 500 } }, 1_162_500n, false],
[{
originalContractCents: 9_007_199_254_740_993n,
priorCertificatesCents: 9_007_199_254_740_992n,
lines: [{ id: 'A', scheduledCents: 9_007_199_254_740_993n, currentWorkCents: 9_007_199_254_740_993n }],
}, 1n, false],
];
for (const [input, due, steppedDown] of cases) {
const result = calculatePayApplication(input);
assert.equal(result.totals.currentPaymentDueCents, due);
assert.equal(result.rates.stepDownApplied, steppedDown);
}
for (let i = 0; i < 4_000; i++) calculatePayApplication(cases[i % cases.length][0]);
const iterations = 60_000;
const times = [];
for (let sample = 0; sample < 5; sample++) {
let checksum = 0n;
const start = performance.now();
for (let i = 0; i < iterations; i++) {
checksum += calculatePayApplication(cases[i % cases.length][0]).totals.currentPaymentDueCents;
}
times.push(performance.now() - start);
assert.equal(checksum, 56_343_750_000n);
}
const medianMs = times.sort((a, b) => a - b)[2];
console.log({
node: process.version, platform: process.platform, arch: process.arch,
iterations, medianMs: Number(medianMs.toFixed(2)),
applicationsPerSecond: Math.round(iterations * 1000 / medianMs),
});
JS
On arm64 macOS with Node.js 24.15.0, the median of five runs was 45.96 ms for 60,000 applications, or about 1.31 million per second on this fixture mix.
Keep the two retainage categories in the audit trail
Round each line's work and stored-material retainage independently, then sum the rounded cents. At 50%, each of two 1-cent lines retains 1 cent, for a total of 2 cents. Pooling them first would retain only 1 cent in total. Reclassify installed materials by moving their value from stored to work, so gross billing stays constant while the applicable retainage can change.
The demo loads the same cutoff on both sides of 50%. Switch presets to compare the gross billed amount, retained amount, and current due. Interactive GitHub Pages demo · Progress billing · Change orders
Top comments (0)