开发者

Javascript Optimization, where to begin?

开发者 https://www.devze.com 2023-01-25 05:08 出处:网络
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

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 cli开发者_运维百科ent 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?


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.


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.


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)


You have a long road ahead of you mate, and I dont envy you.

Here are some Performance Optimization Techniques for Javascript that I wrote down after working in a similar role as yours recently.

They are broken down into 5 broad categories in order of the performance difference they make.

However given what you said about the codebase, I think the second section on Managing and Actively reducing your Dependencies is the most relevant, particularly:

  1. Modifying code to reduce library dependencies, and
  2. Using a post-load dependency manager for your libraries and modules

However all 25 techniques listed there are useful for improving performance.

I hope that you find them useful.

0

精彩评论

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