Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Optimization, where to begin?

I've started work recently at a new company and they have an existing application with 1000s of lines of Javascript code. The baseline contains dozens of JS files with easily over 10,000 custom lines of code, they also use multiple 3rd party libraries such as Jquery, Livequery, JQTransform and others. One of the major complaints they have been receiving from users is the slowness of the client side operation of the site. I've been tasked with optimizing and improving the performance of the JS. My first step will be obviously to move forward to the newest Jquery library, and incorporate JSMin into the build process. Other than that I'm wondering if anyone has some tips on where to begin with optimization on such a huge code base?

like image 589
Omikron Theta Avatar asked Nov 14 '10 14:11

Omikron Theta


People also ask

What is JavaScript optimization?

Optimization is a special type of JavaScript minification. These kind of minimizers not only delete unuseful white spaces, commas, comments etc. but also help to avoid “dead code”: Google Closure Compiler.


3 Answers

You could try installing DynaTrace Ajax Edition (free download here) and see what that tells you. It supports only IE8 I think, but that's probably as good a place to start as any. It's got a much more thorough and understandable profiler interface than do either Firebug or Chrome, in my opinion.

One thing that jumps out at me is "Livequery", which if not used very carefully can cause gigantic performance problems.

Remember this: in a code base that big, developed over time and possibly not with the most "modern" Javascript techniques available, your real problems are going to be bad algorithms in your own code. Newer libraries and minification/optimization methods are good ideas, but the first thing you need to do is find the pages that seem sluggish and then start profiling. In my experience, in a big old codebase like that, you'll find something terrible really quickly. Install a desktop gadget that tracks CPU utilization. That's a great way to see when page code is causing the browser to slow down directly, and not just network lag. Any big spike in browser CPU usage for any significant amount of time should be a big red flag.

like image 119
Pointy Avatar answered Oct 03 '22 15:10

Pointy


Profile that code. Don't optimize something if you just "feel" it could be optimized. Remember the 80% 20% rule. 80% of time is spent in 20% of code.

Use Google's Closure tools. They can optimize and reduce your JS code, which will at least cause it to load faster on your client's computers.

like image 32
darioo Avatar answered Oct 03 '22 16:10

darioo


The way to go is to find bottlenecks. If you find the actual situtation where the app is slow, you can use Firebug to profile your code and tell how much time spent on every function and how many times they have been called. From this information it's pretty easy to determine what areas need some improvement.

Generally the bottlenecks of a webapplication are:

  • Working with the DOM extensively (repaints, reflows)
  • Heavy network communication (AJAX)
like image 38
25 revs, 4 users 83% Avatar answered Oct 03 '22 16:10

25 revs, 4 users 83%