bun icon indicating copy to clipboard operation
bun copied to clipboard

SetTimeout / Sleep Issue with bun.

Open oridgway opened this issue 4 months ago • 1 comments

What version of Bun is running?

1.2.16, although same issue in 1.2.15

What platform is your computer?

Linux 6.14.7-202.nobara.fc42.x86_64 x86_64

What steps can reproduce the bug?

// index.ts
const tickRate = 1000 / 60;

let last = performance.now();

function tick() {
        const now = performance.now();
        const delta = now - last;
        last = now;

        console.log(`Tick at ${now.toFixed(2)} (Δ${delta.toFixed(2)}ms)`);

        const timeTaken = performance.now() - now;
        const sleepTime = Math.max(1, Math.ceil(tickRate - timeTaken));

        setTimeout(tick, sleepTime);
}

tick();

However its the same problem when using

const tickRate = 1000 / 60;

let last = performance.now();

async function Tick(): Promise<void> {
	while (true) {
		const now = performance.now();
		const delta = now - last;
		last = now;

		console.log(`Tick at ${now.toFixed(2)} (Δ${delta.toFixed(2)}ms)`);

		const timeTaken = performance.now() - now;
		const sleepTime = Math.max(1, Math.ceil(tickRate - timeTaken));
		await Bun.sleep(sleepTime);
	}
}

await Tick();

What is the expected behavior?

Tick at 44.88 (Δ0.02ms)
Tick at 61.76 (Δ16.89ms)
Tick at 79.05 (Δ17.28ms)
Tick at 96.24 (Δ17.19ms)
Tick at 113.43 (Δ17.19ms)
Tick at 130.81 (Δ17.38ms)
Tick at 148.02 (Δ17.21ms)
Tick at 165.30 (Δ17.28ms)
Tick at 182.62 (Δ17.33ms)
Tick at 200.10 (Δ17.48ms)
Tick at 217.39 (Δ17.29ms)
Tick at 234.57 (Δ17.19ms)
Tick at 251.73 (Δ17.16ms)
Tick at 268.90 (Δ17.17ms)
Tick at 286.08 (Δ17.18ms)
Tick at 303.23 (Δ17.15ms)
Tick at 320.39 (Δ17.16ms)
Tick at 337.57 (Δ17.18ms)
Tick at 354.73 (Δ17.16ms)
Tick at 371.91 (Δ17.18ms)
Tick at 389.08 (Δ17.17ms)
Tick at 406.26 (Δ17.18ms)
Tick at 423.43 (Δ17.17ms)
Tick at 440.60 (Δ17.17ms)
Tick at 457.75 (Δ17.15ms)
Tick at 474.90 (Δ17.15ms)
Tick at 492.07 (Δ17.17ms)
Tick at 509.24 (Δ17.17ms)
Tick at 526.42 (Δ17.17ms)
Tick at 543.59 (Δ17.17ms)

What do you see instead?

Tick at 4.96 (Δ0.06ms)
Tick at 22.70 (Δ17.75ms)
Tick at 40.40 (Δ17.70ms)
Tick at 72.45 (Δ32.04ms)
Tick at 104.50 (Δ32.06ms)
Tick at 1000.35 (Δ895.85ms)
Tick at 2000.36 (Δ1000.01ms)
Tick at 2032.62 (Δ32.26ms)
Tick at 2065.80 (Δ33.17ms)
Tick at 3000.36 (Δ934.56ms)
Tick at 3033.65 (Δ33.29ms)
Tick at 4000.36 (Δ966.71ms)
Tick at 4021.31 (Δ20.95ms)
Tick at 4038.46 (Δ17.15ms)
Tick at 5000.35 (Δ961.89ms)
Tick at 5032.64 (Δ32.29ms)
Tick at 6000.35 (Δ967.71ms)
Tick at 6032.73 (Δ32.38ms)
Tick at 7000.36 (Δ967.63ms)

Additional information

Hi There, I when running the above code in bun, i would expect the a console.log approximately every 16ms. However in bun there is the occasional delay when it take the remainder of the second to run the loop again, like something is blocking the event loop.

When running this with node it chugs along happily at 16-17ms all day.

Tried with both bun 1.2.15 and 1.2.16, and with both Bun.sleep and setTimeout. Same problem if the typescript is compiled with tsc and ran as js.

oridgway avatar Jun 12 '25 23:06 oridgway