简体   繁体   English

事件之外的JavaScript缓慢

[英]JavaScript slowness outside events

How do I diagnose web page performance problems if none of my JavaScript events seem to be the cause? 如果我的JavaScript事件似乎都不是原因,如何诊断网页性能问题?

I have a web application using jqGrid . 我有一个使用jqGrid的Web应用程序。 Clicking on the grid causes a 2-3 second freeze before anything happens (including any click events being hit). 单击网格会导致2到3秒钟的冻结,然后一切都不会发生(包括任何被点击的点击事件)。 Clicking anywhere else on the page is fine (instant response). 单击页面上其他任何地方都可以(立即响应)。 Removing rows reduces the delay (50 rows is very slow, but 5 rows yields an instant response). 删除行可减少延迟(50行非常慢,但5行会产生即时响应)。

Unbinding all events on the page à la $('*').add(document).unbind() doesn't fix the problem, which leads me to believe there are events I'm missing. 解除页面àla $('*').add(document).unbind()上所有事件的$('*').add(document).unbind()并不能解决问题,这使我相信有些事件我丢失了。

I don't see this slow behavior if I save the page source to my desktop and open that HTML file in my browser. 如果将页面源保存到桌面并在浏览器中打开HTML文件,我看不到这种缓慢的行为。 That seems to indicate that this is in fact a JavaScript issue. 这似乎表明这实际上是一个JavaScript问题。

I'm testing with Firefox 3.6.8 and the latest version of Firebug. 我正在使用Firefox 3.6.8和最新版本的Firebug进行测试。 The problem also affects other browsers, but less so in the fast ones (Safari and Chrome). 该问题还影响其他浏览器,但对于快速浏览器(Safari和Chrome)影响较小。

Update : IE7 doesn't seem to suffer from this lag, surprisingly. 更新 :令人惊讶的是,IE7似乎没有遭受这种滞后的困扰。 I put an alert at the very beginning of the jqGrid click event, and the event box appeared immediately after I click on the grid. 我在jqGrid click事件的开始处发出了警报,并且在单击网格后立即出现了事件框。 Firefox still has a multi-second lag, and Chrome and Safari have a very short lag (not quite as short as IE, but still very short). Firefox仍然有几秒钟的滞后,而Chrome和Safari的滞后很短(虽然不如IE短,但仍然很短)。

Update #2 : I'm fairly confident this may be a bug in Firefox. 更新#2 :我非常有信心这可能是Firefox中的错误。 Even though I'm unbinding/die'ing all events, I'm guessing that Firefox isn't properly cleaning up everything. 即使我取消绑定/死掉所有事件,但我猜测Firefox无法正确清理所有事件。 I removed all other events and added a single click event to the grid table and started the profiler. 我删除了所有其他事件,并向网格表添加了一个单击事件,然后启动了事件探查器。 I then clicked once on the table and ended the profiler. 然后,我在表上单击一次,然后结束事件探查器。 I got back a "No activity to profile." 我回来了“没有活动可记录。” message. 信息。

The best tool I can recommend for this is dynaTrace AJAX Edition , it's a profiler for IE (typically the slowest in JS performance, so actually a good test environment for starting optimization). 我为此推荐的最佳工具是dynaTrace AJAX Edition ,它是IE的探查器(通常是JS性能中最慢的,因此实际上是启动优化的良好测试环境)。

It'll give you a full breakdown, show you repeated method calls, the call tree and where your CPU cycles are going. 它会给您完整的细分,向您展示重复的方法调用,调用树以及您的CPU周期。 Check out their tutorials on usage/features to get started. 查看他们的用法/功能教程以开始使用。

Oh yeah, it's free :) 哦,是的,它是免费的:)

Are you using live() with all those click events? 您是否对所有这些click事件都使用live()? Sometimes live() can really slow down page performance if there are too many instances on a particular page. 如果特定页面上的实例过多,则live()有时确实会降低页面性能。 Especially Ajax relaced content that contains link that live has to rebind to. 尤其是Ajax取代了包含实时链接的内容,该链接必须重新绑定到该内容。

See here for more info: 有关详情,请参阅此处:

Does jquery live slow down websites? jQuery Live是否会减慢网站速度?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM