views:

139

answers:

3

I'm in the process of writing a jQuery plugin, and am getting into some fairly heavy operations, which is making my plugin slower and less responsive. I was wondering if there was any tools out there that would help me optimize my JavaScript?

A: 

Its difficult to say without looking at at any code, ie how efficiently things have been written eg: how you're using selectors to find elements etc.

Have a look at some jQuery tips and tricks blogs.

eg:

http://viralpatel.net/blogs/2009/08/20-top-jquery-tips-tricks-for-jquery-programmers.html http://www.tvidesign.co.uk/blog/improve-your-jquery-25-excellent-tips.aspx

Mark Redman
+8  A: 

I recommend using Firebug's "profile" tab as a start.

Just click the profile tab, then use your plugin for a while and then click the profile tab again. You'll then see a report of what functions were called and how much time each one took.

Then, I recommend the article Speed up your JavaScript

As outlined in the article, why a script can take too long to execute

  1. Too much happening in a loop.
  2. Too much happening in a function.
  3. Too much recursion.
  4. Too much DOM interaction.

I have not yet found a tool that automatically optimizes or refactors JavaScript for speed. It's always been a manual process for me.

Chris Tek
+2  A: 

This Google Talk video goes into a lot of useful detail...

http://www.youtube.com/watch?v=mHtdZgou0qU

Nosredna
+1 - that's a great video
Russ Cam