r/javascript • • Oct 06 '26

AskJS [AskJS] My simulation runs in about 1 second in the terminal but took 13 seconds in a Chrome page. The culprit was setTimeout(0).

I have a page, built with Cline, that runs a game simulation in the browser: 30 batches (one per game mode and bot pair), with a progress line between them. To let the page repaint between batches, I yielded with `await new Promise(r => setTimeout(r, 0))`.

A full run took 13.0 seconds in Chrome. The same work takes about 1 second in the terminal on my laptop.

The clue was that 100 games per bot took just as long as 1,000. The time was not in the games. It was in the pauses, about 0.7 seconds each.

Chrome throttles chained timers in pages it treats as hidden or in the background. After a few nested setTimeout calls, it only fires them about once a second. My first test used 5 timers, showed 0ms, and looked fine, because the throttling only starts after a few nested calls. With 20 the difference was obvious: 20 chained setTimeout(0) calls took 11.5 seconds, and 20 MessageChannel messages took 0ms.

The fix was to yield with a message instead, which is also how React's own scheduler yields:

const nextTick = () => new Promise<void>((resolve) => {

const channel = new MessageChannel();

channel.port1.onmessage = () => {

channel.port1.close();

resolve();

};

channel.port2.postMessage(null);

});

The same run went from 13.0s to 4.2s, still in a window Chrome treated as hidden. I have not timed it in a normal foreground tab yet, so I am not claiming a number there.

Two other details. Leaving the page bumps a run id, so an unfinished run stops instead of setting state after unmount. And a Web Worker would move the work off the main thread entirely. I chose the smaller change because the main-thread version was now fast enough.

So, for CPU-heavy work in a page: do you reach for a Worker first, or yield on the main thread until that stops being enough?

1 Upvotes

14 comments sorted by

16

u/Ronin-s_Spirit Oct 06 '26 • Heartwarming

Why are you using background tabs instead of workers... especially if you want to repeatedly pause them, they have atomics for that.

1

u/Fair-Process-7291 Oct 17 '26

the hidden window wasnt on purpose, its just where i was testing. setInterval gets throttled the same way there. worker is the real fix tho agreed

6

u/profound7 Oct 07 '26 • Heartwarming

How does it compare with requestAnimationFrame?

function loop(timestamp) {
    // ...
    // ...
    requestAnimationFrame(loop);
}

9

u/horizon_games Oct 10 '26 • Heartwarming

Gonna guess OP and his AI don't know about that

1

u/Fair-Process-7291 Oct 17 '26

rAF pauses completely in hidden tabs tho, and thats where it was running so it would've just stopped

16

u/ijblack Oct 07 '26 • Heartwarming

this post was definitely written by claude

11

u/Ronin-s_Spirit Oct 06 '26 • Heartwarming

Why are you yielding messages and promises? Why is your game loop not running inside an interval?

1

u/pmickael_584 28d ago

Worker first here: the page only needs a progress line between independent batches, so the simulation doesn't need to share the UI thread. MessageChannel dodges timer clamping; it doesn't stop the work competing with the UI.

0

u/Muted-Associate-7512 Oct 06 '26 • Heartwarming

Huh I always assumed setTimeout 0 was basically free, that nesting clamp is sneaky. Worker first for me, but MessageChannel is a good middle step when you just need the UI to breathe.

0

u/horizon_games Oct 10 '26 • Heartwarming

Yiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii

0

u/horizon_games Oct 10 '26

kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkeeeeeeeeeeeeeeeeeeeeeeee

0

u/horizon_games Oct 10 '26

ssssssssssssssssssssssssssssssssss

2

u/[deleted] Oct 07 '26 • Heartwarming

[removed] — view removed comment

1

u/Fair-Process-7291 Oct 17 '26

this is super helpful thanks. yeah the 4.2s was in a hidden window so "fast enough" was kinda generous lol. worker is probably next