开发者

Reducing JQuery Size by Removing Unnecessary Functions

I am using only fadeIn and fadeOu开发者_运维知识库t from jQuery and I don't want to load the whole jQuery. Is there any way to remove other functions? I know that minified version of jquery is really small but 1KB matters in my case.


This is part of the decision on whether to use a javascript framework or not. Every framework contains a base, and that base should not be interfered with. If you edit the core of a framework, you've essentially removed the primary benefit of using the framework to begin with. Now, you cannot update when new versions are released without re-hacking the framework again.

If size is a key consideration in your project, then it was a bad decision to include a framework that you apparently barely needed. Next time, you'll know to base this decision on more than a handful of novelty effects. :)


No, you should absolutely not attempt to hack out the parts of jQuery you don't need. Someday you might want to upgrade to the latest jQuery, or add some small bit of functionality to your site that depends on something you removed. You'll either spend a lot of time undoing your changes or start fresh and have to hack out the unwanted stuff again.

There are far more effective ways of making jQuery load faster and with less bandwidth. Do what the jQuery website itself does and use a CDN. Taken directly from the source at jquery.com:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> 

Chances are very good this file will be cached by the browser. No amount of hacking/minifying will equal the speed gains you'll get by loading the file from the browser's cache. It is simply the easiest and most effective way to load jQuery quickly.


A suggestion too, older versions of jquery are smaller. You lose some features but can still utilize a lot of the benefits with a smaller footprint. Take jquery 1.2.3:

URL: https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js Title: No title found Date: Report run on Fri Feb 18 13:43:55 EST 2011 Total Size: 15958 bytes

Vs 1.5.0:

URL: https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js Title: No title found Date:Report run on Fri Feb 18 13:46:10 EST 2011 Diagnosis Global Statistics Total Size: 29466 bytes


If you are using one or two features of a framework better try to write your own function which can fulfill your need, this way, you will gain experience as well as full control over the function

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜