简体   繁体   English

javascript的高CPU使用率 - 如何调试?

[英]High CPU usage of javascript - How to debug?

What is the best way to find the cause of high cpu usage of javascript? 找到javascript的高CPU使用率的最佳方法是什么?

I have a script that simply loads photos from flickr in a thumbnail gallery. 我有一个脚本,只需在缩略图库中加载来自flickr的照片。 After they're loaded, nothing is being done (waiting for you to click them to display in a lightbox), but the cpu usage is still 25% or more. 加载后,没有任何操作(等待您点击它们以显示在灯箱中),但CPU使用率仍然是25%或更多。

I've opened firebug, clicked profile, waited a bit then clicked it again, but it says "No activity to report". 我打开了firebug,点击了个人资料,稍等了一下再点击它,但它说“没有活动要报告”。 If there was something going on, wouldn't it report? 如果有什么事情发生,它会报告吗? Or am I using this tool wrong? 或者我使用这个工具错了?

I am doing this after everything is loaded, just to see what might be causing that high cpu usage. 我在加载所有内容后执行此操作,只是为了查看可能导致高CPU使用率的原因。

Thanks, Wesley 谢谢,韦斯利

Edit: Seem to have traced it's origin.. It's not anything JS related, but CSS! 编辑:似乎已经追溯了它的起源......它与JS无关,但是CSS! WHAT THE HELL? 搞什么鬼?

The issue is this: 问题是:

I have a thumbnail gallery, I display a loading indicator instead of the thumbnail whilst the image is loading: 我有一个缩略图库,我在图片加载时显示加载指示器而不是缩略图:

.thumbnail img {
   display:block;
   background-image: url("/images/ajax-loader.gif");
   background-position: center center; 
   background-repeat:no-repeat;
   overflow:hidden;
   color:white;
}

If I remove this code, the cpu usage drops to 0,03% or something.. instead of 20%... Is this because of the animated gif that is still playing, but just covered up? 如果我删除这个代码,CPU使用率下降到0,03%或者什么......而不是20%......这是因为GIF动画还在播放,但只是掩盖了吗?

I did not know that animated gifs take this much from the cpu? 我不知道GIF动画从cpu中拿了这么多? Granted, it's doing that more than 20 times, but still. 当然,它已经做了20多次,但仍然如此。 It was the same in safari/chrome as well. 在safari / chrome中也是如此。

I guess I should either not use the loading indicator thing inside of each image, or remove the background-image on a successful load of the image.. 我想我应该不使用每个图像内部的加载指示器,或者在成功加载图像时删除背景图像。

I have used over the last weeks the tool "dynaTrace AJAX Edition" (free tool) for analyzing web sites (not only JavaScript). 我在过去几周使用了工具“dynaTrace AJAX Edition” (免费工具)来分析网站(不仅仅是JavaScript)。 It has some pretty good views that show the usage of resources. 它有一些非常好的视图,显示资源的使用。 Give it a try to track down the part of the javascript (or other parts of the browser) that are the reason for high cpu usage. 尝试跟踪jpu(或浏览器的其他部分)中导致高CPU使用率的部分。 However the tool is only working with firefox and current internet explorer implementations, but when you use firebug, that seems to indicate that you are using firefox. 但是该工具仅适用于firefox和当前的Internet Explorer实现,但是当你使用firebug时,这似乎表明你正在使用firefox。

There is an entry on their forums page named "What else can impact Browser CPU Usage and AJAX Performance?" 他们的论坛页面上有一个名为“还有什么可以影响浏览器CPU使用率和AJAX性能?”的条目。 which gives some more hints that may help. 这提供了一些可能有用的提示。

You could try Chrome/Chromium Developer Tools » Profiler, Start, reload the page, wait a few minutes and stop it. 您可以尝试使用Chrome / Chromium开发人员工具»Profiler,启动,重新加载页面,等待几分钟然后停止。 It would be enough data to have a clue of what could be the problem. 有足够的数据可以找出可能出现问题的线索。

Another common issue with high cpu usage on browsers it's css animations, (as well as gif images). 在浏览器上使用高cpu的另一个常见问题是css动画(以及gif图像)。 I've been stuck with a high cpu usage issue when developing a web application, it was weird that it only happened when the tab was active/visible, but when on background it didn't, and the problem were present on FF, Chrome and Chromium. 在开发Web应用程序时,我一直遇到高CPU使用率问题,奇怪的是它只发生在标签处于活动/可见时,但是在背景上它没有,并且问题出现在FF,Chrome上和铬。 Finally it turned to be animated Bootstrap Progress bar. 最后它变成了动画Bootstrap Progress栏。

You could check this easily here http://getbootstrap.com/components/#progress-animated , open it on Chrome, open Tools » Task Manager, then Toggle animation on Bootstrap site and check cpu usage. 您可以在http://getbootstrap.com/components/#progress-animated上轻松查看,在Chrome上打开它,打开工具»任务管理器,然后在Bootstrap站点上切换动画并检查cpu使用情况。

I guess this is not strictly an answer to the question, but a usefull tip to find the problem. 我想这不是问题的严格答案,而是找到问题的有用提示。 Since I can't comment yet, I decided to post it as an answer as someone could find it usefull, let me know if this is such a bad practice. 由于我还没有发表评论,我决定将其作为答案发布,因为有人可能会发现它很有用,请告诉我这是不是很糟糕的做法。

Try to reload the page after enabling FireBug profile feature. 尝试在启用FireBug配置文件功能后重新加载页面。

I heavilly used FireBug profile feature to do this. 我很喜欢使用FireBug配置文件功能来执行此操作。 You can see which function is consuming most of the time. 您可以在大多数情况下查看哪些功能正在消耗。 Great thing. 太好了。 Try this on other pages with simple javascript example. 使用简单的javascript示例在其他页面上尝试此操作。 It should work well. 它应该运作良好。 If it doesn't work in your example, just post them a bug. 如果它在您的示例中不起作用,只需向它们发布一个错误。

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

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