Skip to content

FrameBudget

FrameBudget tracks a wall-clock deadline for optional work such as queue draining. It is independent of FrameScheduler.maxStepsPerFrame and the scheduler's panic state.

ts
import { FrameBudget } from "@jolly-pixel/loop";

const budget = new FrameBudget();

budget.start(4);
while (queue.length > 0 && !budget.expired) {
  rebuild(queue.shift());
}

Constructor

new FrameBudget(clock)

ts
new FrameBudget(clock?: Clock);

The constructor uses PerformanceClock by default and accepts a ManualClock for tests.

Properties

PropertyDescription
budgetMilliseconds granted by the last start().
elapsedMilliseconds since the last start(), or 0 when cleared.
remainingMilliseconds left, clamped to 0.
expiredtrue before start() and once the deadline is reached.

API

start(budgetMs: number): this starts a deadline at the clock's current time. It throws RangeError when budgetMs is negative or non-finite.

clear(): this resets the budget and makes it expired.

A budget of 0 expires immediately. A fresh or cleared budget is also expired, so optional work should only run after start().