Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reducing JQuery Size by Removing Unnecessary Functions

I am using only fadeIn and fadeOut 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.

like image 725
AliBZ Avatar asked Feb 18 '11 18:02

AliBZ


4 Answers

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. :)

like image 177
Chris Baker Avatar answered Nov 23 '22 22:11

Chris Baker


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.

like image 40
meagar Avatar answered Nov 24 '22 00:11

meagar


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

like image 23
Jage Avatar answered Nov 23 '22 23:11

Jage


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

like image 37
pahnin Avatar answered Nov 23 '22 22:11

pahnin