Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
522 views
in Technique[技术] by (71.8m points)

firefox - Javascript performance when running in an unfocused tab

It seems like there has been a change to some recent version of Chrome and Firefox*, and now Javascript execution seems to be different when the tab it is being run in is not the currently focused one.

When I run my Javascript unit tests, they normally take about 20 seconds to complete but now, when the tab is unfocused, it takes upwards of 2000 seconds. What is strange though, is that the run times for each individual test are not affected (most are still < 10ms). The test runner I'm using adds a setTimeout(0) between running each test so that the browser doesn't lock up while executing, and so that seems the likely culprit.

Is there a way to tell the Javascript engine not to "deprioritise" that tab though? It's nice to be able to run my tests in the background without having to watch myself...

*sorry, I don't really care enough to try installing old versions to find when this started happening. At the very least, it's happening now on Firefox 5.0 and Chrome 12.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

setTimeout and setInterval have been throttled to a minimum of 1000ms in unfocused tabs. Here is the Bugzilla report that mentions it. And here is the similar Chromium bug report. I believe this is the case in Firefox 5 and in Chrome since version 11.

According to MDN:

In (Firefox 5.0 / Thunderbird 5.0) and Chrome 11, timeouts are clamped to firing no more often than once per second (1000ms) in inactive tabs; see bug 633421 for more information about this in Mozilla or crbug.com/66078 for details about this in Chrome.

As for getting around this restriction, you could try the technique discussed in this article, but I haven't had a change to try it myself yet.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...