Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does web worker do when owner not in focus?

[edit: this question MAY need narrower scope, so I will state that I am currently using Chrome 49.0.2623.112 m (64-bit) with default settings when I notice this. I am currently using HTTPS, but I observed it before the switch.]

I have an application that hands AJAX polling off to web workers. It's not an incredible savings in terms of performance, but it makes the application more easily reasoned and has potential for future performance benefits.

I add a console logging line to the worker so that it just tells me every time the XHR request is made. As you may know, when duplicates of logging lines come into Chrome, the Inspector just records the number of duplicates.

As I view the page, I can see in the console: (52) XHR invoked

If I have the page in focus, I see that integer increase every 2 seconds, per my polling interval.

HOWEVER,

When I switch to another tab for a while, and then return, it takes several seconds for that number to update, at which time it only updates by one. That might be because the worker is actually pausing, which is fine in this particular application, or it might be that the worker is still running but the console API just can't receive the logging line from the worker if the parent isn't visible. Not sure.

But the important thing I'm trying to solve is the delay when I return to the tab. If it's a responsively updating dashboard, a delay of 10-60 seconds before updates continue is noticeable and is a UI performance bug.

Can anybody shed some insight into what happens with the web worker when the owner tab loses focus? Is it actually queueing up each instance of the work that needs to be done (on XHR message) and then doing it when the owner comes back?

Would manually pausing the polling mechanism on lost focus help anything?

like image 917
Greg Pettit Avatar asked Oct 18 '25 01:10

Greg Pettit


1 Answers

This behaviour will vary between browsers. Most of them will reduce JavaScript priority to preserve CPU cycles and performance overhead if the tab is not used. I believe some browsers suspend the tab execution entirely so yes it will be a pause until back in focus. I think Firefox has an option to control this.

like image 122
Lambros Avatar answered Oct 19 '25 14:10

Lambros