Skip to content

FrameBudget ​

A time limit for optional work inside one frame.

Use it when you drain a queue (mesh rebuilds, uploads) and want to stop after a few milliseconds.

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

const budget = new FrameBudget();

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

It has no link to the scheduler. maxStepsPerFrame and panics are unaffected.

Constructor ​

ts
new FrameBudget(clock?: Clock);

Defaults to a PerformanceClock. Pass a ManualClock in tests.

Methods ​

MethodWhat it does
start(budgetMs): thisStarts a deadline budgetMs from now. Throws RangeError when negative or not finite.
clear(): thisDrops the deadline. The budget is expired again.

Properties ​

PropertyMeaning
budgetMilliseconds given to the last start().
elapsedMilliseconds since start(). 0 after clear().
remainingMilliseconds left, never below 0.
expiredDeadline reached.

Gotchas ​

  • A new or cleared budget is already expired. Call start() first, or the loop above runs nothing.
  • start(0) expires immediately.