LibWeb: Implement timer nesting level throttling

This clamps the interval of repeating timers to 4ms after they have run
5 times. This prevents CPU spinning for `setInterval()` calls with low
timeouts.
This commit is contained in:
Tim Ledbetter
2026-02-02 20:03:53 +00:00
committed by Tim Ledbetter
parent 24055b60ef
commit 7577fd2a57
Notes: github-actions[bot] 2026-02-11 16:34:54 +00:00
4 changed files with 54 additions and 9 deletions

View File

@@ -0,0 +1,23 @@
<!DOCTYPE html>
<script src="../include.js"></script>
<script>
asyncTest(done => {
// With 4ms clamping after nesting level 5, running for 100ms should yield
// roughly 5-6 unclamped calls plus approximately 25 clamped calls.
// Without clamping, we'd get thousands of calls.
// We test that the count is bounded, proving clamping is working.
const testDurationMilliseconds = 100;
let callCount = 0;
const id = setInterval(() => {
callCount++;
}, 0);
setTimeout(() => {
clearInterval(id);
println(`Callback count bounded (< 100 in 100ms): ${callCount < 100}`);
done();
}, testDurationMilliseconds);
});
</script>