v153 · loaf in workers
The workaround, and what it cannot tell you
There is a way to notice a blocked worker from inside it: reschedule a task continuously and watch for one that runs late. It works, it costs almost nothing, and comparing what it produces against a real Long Animation Frame entry shows exactly what is missing.
Block the worker, then compare the two records
| # | blocked for | at |
|---|---|---|
| Not run yet. | ||
The self-timer is nine lines: a setTimeout(…, 16) that reschedules itself and records any gap over 50ms. That is the entire workaround, and its total cost is one small task every 16ms. (A 0 delay would also work, but its clamped 4ms chain never lets the worker go idle, which in Chromium keeps a congested moment open and delays the real entry.) While the worker is blocked, this page keeps posting it a message every 40ms, as a busy app would; that queued work is what Chromium's worker implementation reports.
Field by field
| field | the entry | the workaround | why it matters |
|---|
The workaround in full
// Inside the worker.
let lastTaskEnd = performance.now();
const tick = () => {
const now = performance.now();
const gap = now - lastTaskEnd;
if (gap > 50) report({ blockedFor: gap, at: now });
lastTaskEnd = performance.now();
setTimeout(tick, 16);
};
setTimeout(tick, 16);
Note what it measures: the gap between one of its own tasks and the next. If a single task blocks for 400ms, the gap is 400ms and the timer is correct. If four tasks each block for 100ms with the timer running in between, the timer reports nothing at all — every individual gap is under the threshold, and the worker was still unresponsive for 400ms.