简体   繁体   English

如何跟踪慢JS或JQuery代码

[英]How to trace slow JS or JQuery code

I created a web page for viewing images. 我创建了一个用于查看图像的网页。 This page has some other code that gets included that I did not write. 这个页面有一些其他代码,包括我没有写。 The page loads 40 small images upon load. 该页面在加载时加载40个小图像。 Then the user will scroll down and additional pages of 40 images can be loaded via ajax. 然后用户将向下滚动,并且可以通过ajax加载40张图像的附加页面。 Once I get to 15-20 pages, I notice the page begins to slow significantly. 一旦我达到15-20页,我注意到页面开始显着减慢。 I check app counters and it can go up to 100% cpu and memory can go over 3GB. 我检查应用程序计数器,它可以达到100%的CPU和内存可以超过3GB。 Then I will inevitably get the modal that JQuery is taking too long to execute, asking me if I want to stop executing the script. 然后我将不可避免地得到JQuery执行时间太长的模态,问我是否要停止执行脚本。 Now I realize that a page with up to 800 images is a big load, but the issue with JQuery suggests to me that some code may also be iterating over this larger and larger group of dom objects. 现在我意识到一个包含多达800个图像的页面是一个很大的负载,但是JQuery的问题告诉我,一些代码也可能正在迭代这个越来越大的dom对象组。 It almost appears to get exponentially slower as I pass 15 pages or so. 当我通过15页左右时,它几乎看起来会呈指数级变慢。 Once I get to 20 pages it becomes almost unusable. 一旦我达到20页,它几乎无法使用。

First of all, is it even possible to run a page efficiently, even with minimal JS, when you have this many images? 首先,当你拥有这么多图像时,即使使用最少的JS,它甚至可以有效地运行页面吗? Secondly, is there a recommended way to "trace" JS and see what kinds of functions are getting executed to help determine what is the most likely culprit? 其次,是否有推荐的方法来“跟踪”JS,看看有哪些功能被执行以帮助确定最可能的罪魁祸首? This is most important to me - is there a good way to do in Firebug? 这对我来说最重要 - 在Firebug中有一个很好的方法吗?

Thanks :) 谢谢 :)

EDIT - I found my answer. 编辑 - 我找到了答案。 I had some older code which was being used to replace images that failed to load with a generic image. 我有一些旧的代码,用于替换无法加载通用图像的图像。 This code was using Jquery's .each operator and thus was iterating over the entire page and each new ajax addition every time the page loaded. 这段代码使用的是Jquery的.each操作符,因此每次加载页面时都会迭代整个页面并添加每个新的ajax。 I am going to set a class for the images that need to be checked in CSS so that the ajax-loaded images are unaffected. 我将为需要在CSS中检查的图像设置一个类,以便加载ajax的图像不受影响。

Firebug, and all the other debugging tools let you profile your functions. Firebug和所有其他调试工具可让您分析您的功能。 You can see how long they take to run and how many times they have been called. 您可以看到它们运行了多长时间以及它们被调用了多少次。

http://getfirebug.com/javascript http://getfirebug.com/javascript

See: Profile JavaScript performance 请参阅:Profile JavaScript性能

Another useful tool to look into is the profile() function 另一个有用的工具是profile()函数

console.log('Starting Profile');
console.profile();
SuspectFunction();
console.profileEnd();

Though the console window in the debugger you can see the profile results. 虽然调试器中的控制台窗口可以看到配置文件结果。

我使用过的最好的工具是https://developers.google.com/web-toolkit/speedtracer/ for Chrome

To answer your first question, 15 pages of images should not be a problem for a computer to handle. 要回答您的第一个问题,15页图像不应成为计算机处理的问题。 Google loads up to 46 pages of images without lagging at all. 谷歌最多可以加载46页图像而不会有任何滞后现象。 Although it does stop you from loading more after that. 虽然它确实阻止你在此之后加载更多。

The answer your second question, there are many ways to track JS code. 答案是你的第二个问题,有很多方法可以跟踪JS代码。 Since you are doing a performance related debugged, I'd go with timestamped console log: 由于您正在执行与调试相关的性能,因此我将使用带时间戳的控制台日志:

console.log(" message " + new Date()); console.log(“message”+ new Date());

I'd put one in the beginning and end of function you are interested in measuring the performance of, and read through the log to see how long it takes to execute each of those functions. 我将一个放在函数的开头和结尾,您有兴趣测量它的性能,并通读日志以查看执行每个函数所需的时间。 You would compare the timestamp to see what excess code is executing and how long it took for the code to execute. 您可以比较时间戳,以查看正在执行的多余代码以及执行代码所需的时间。

Finally, in Firebug, go to the console tab, and click on Pofile before you start you start scrolling down the page. 最后,在Firebug中,转到控制台选项卡,然后在开始向下滚动页面之前单击Pofile。 Then scroll to your 15th page, and then click Profile again. 然后滚动到第15页,然后再次单击“配置文件”。 It breaks down function called and amount of time it took. 它打破了所调用的功能和所花费的时间。

I prefer to use the timer function in Firebug or Chrome, it can be called like this: 我更喜欢在Firebug或Chrome中使用计时器功能,它可以像这样调用:

   console.time('someFunction timer');

   function someFunction(){ ... }

   console.timeEnd('someFunction timer');

This isn't as robust as the profiler functions, but it should give you an idea of how long functions are taking. 这不像分析器功能那样强大,但它应该让您了解功能需要多长时间。

Also if you are running at 100% CPU and 3GB of memory, you almost certainly have a memory leak. 此外,如果您运行100%CPU和3GB内存,几乎肯定会有内存泄漏。 You may want to consider removing some the initial images, when more pages are loaded in. For example, after 5 pages being shown, you remove the first page when the user views the 6th page. 当加载更多页面时,您可能需要考虑删除一些初始图像。例如,在显示5页后,当用户查看第6页时,您将删除第一页。

I was able to fix the problem by going over my code again. 我能够通过再次检查我的代码来解决问题。 I was loading new images via ajax but I had an older line of code that was checking all images, ie $('img') to replace any images that failed to load with a generic image. 我正在通过ajax加载新图像,但我有一个较旧的代码行检查所有图像,即$('img')替换任何无法加载通用图像的图像。 This means that as I continually load new images, this selector has to iterate over the entire growing dom again and again. 这意味着当我不断加载新图像时,这个选择器必须一次又一次地遍历整个不断增长的dom。 I altered that code and now the page is flying! 我修改了代码,现在页面正在飞行! Thanks everyone for the help. 谢谢大家的帮助。

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

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