在未聚焦的选项卡中运行时的Javascript性能 [英] Javascript performance when running in an unfocused tab

查看:113
本文介绍了在未聚焦的选项卡中运行时的Javascript性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看来近期版本的Chrome和Firefox *已经发生了变化,现在Javascript执行似乎不同于它正在运行的标签不是当前焦点的那个。



当我运行我的Javascript单元测试时,他们通常需要大约20秒才能完成,但现在,当选项卡未聚焦时,需要2000秒以上的时间。但奇怪的是,每个单独测试的运行时间不受影响(大部分仍然<10毫秒)。我正在使用的测试运行器在运行每个测试之间添加了一个 setTimeout(0),以便浏览器在执行时不会锁定,所以这似乎是可能的罪魁祸首。

有没有办法告诉Javascript引擎不要deprioritise那个标签呢?很高兴能够在后台运行我的测试,而不必看自己...

* 对不起,我真的不太在乎安装旧版本以查找何时开始发生。至少现在发生在Firefox 5.0和Chrome 12上。

在未聚焦的选项卡中,setTimeout 和 setInterval 已被限制为最少1000毫秒。 这里是Bugzilla提到的报告。以下是类似的铬错误报告。我相信这是Firefox 5和Chrome 11以后的情况。



根据中使用DOM / window.setTimeout#Inactive_tabs> MDN
$ b


Chrome 11,超时被限制为
,在不活动的选项卡中每超过
秒(1000毫秒)触发不超过一次;请参阅
bug 633421了解更多关于
这个在Mozilla中的信息,或者在
中详细介绍这个在Chrome中的crbug.com/66078。


至于绕过此限制,您可以尝试本文中讨论的技巧,但我还没有改变,自己尝试一下。


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.

解决方案

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.

这篇关于在未聚焦的选项卡中运行时的Javascript性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆