开发者

Detect slow Javascript

Is there any way to detect if Javascript code is taking more time to execute than it should? I don't want to run code, then determine the time it took, I want to determine if actively running code is going slower than it开发者_StackOverflow should, and stop that whole script.


I really liked Nicholas Zakas's presentation titled Speed Up Your JavaScript. Some points to mention from it:

  • Try avoiding accessing the DOM too much. If you're accessing a variable in the DOM (such as document.getElementById('blah), store it in a local variable.
  • Avoid using the global namespace, and use closures only when you need them. Learn how the stack works in JavaScript and you'll see how this makes sense.
  • Counting down in for loops tends to be faster
  • Avoid while, and the catch part of try/catch. For/for in seem to have some performance issues as well.
  • Be wary of HTMLCollection objects.

This information is a couple years old, but there are a lot of good practices in the presentation.


There's no concrete measure of how long a script "should" run. If you want to find where your code is slowest though, I would use a profiler.

FireBug has one and so do most other developer tools.


The YSlow add-on is the best solution to know why your website is slow.

Detect slow Javascript

There are many issues which could be the reason for slowness.

Combining many jQuery to one could help you increasing your performance.

Also you can put the script at the bottom of your page and CSS at top.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜