Does Web Worker throttles setTimeout() / setInteval()?

Tags: , , ,



I have a script on foreground tab that starts (dedicated) web worker. Now I see that setTimeout(xxx, 100) in that web worker is limited to be triggered not more often than once per second instead of 10 times per second as required. I’ve googled such a limitation for inactive tabs but are there any docs that say the same about Web Workers? I’ve checked this in Chrome and Firefox.

Answer

There is no real specs about that throttling behavior, even though they do allow it:

  1. Optionally, wait a further implementation-defined length of time.

This is intended to allow user agents to pad timeouts as needed to optimize the power usage of the device. For example, some processors have a low-power mode where the granularity of timers is reduced; on such platforms, user agents can slow timers down to fit this schedule instead of requiring the processor to use the more accurate mode with its associated higher power usage.

Chrome has this document from 2017 where they expose how they planned to handle background tabs.

From there we can see a plan section about Web Workers:

Throttling web workers (~2018)

We are currently instrumenting web workers to better understand their CPU usage and power impact. We consider throttling web workers as well, being thoughtful about minimizing broken content.

So that was scheduled for 2018, and here is the issue tracking it’s deployment.

Should also be noted that there is currently an active discussion going on on the WICG to implement the Page-Lifecycle API. This API will expose a few events, and a new “frozen” state, which for what we are interested, should also pause Workers’ execution.



Source: stackoverflow