Minggu, 25 Maret 2012

Tips and Tricks Optimize jQuery Powerful For Developers

1) Use the Latest Version of jQuery

With all the innovation taking place in the jQuery project, one of the easiest ways to improve the performance of your web site is to simply use the latest version of jQuery. Every release of the library introduces optimizations and bug fixes, and most of the time upgrading involves only changing a script tag.
You can even include jQuery directly from Google’s servers, which provide free CDN hosting for a number of JavaScript libraries.
1<!-- Include a specific version of jQuery -->
3
4<!-- Include the latest version in the 1.6 branch -->
The latter example will include the latest 1.6.x version automatically as it becomes available, but as pointed out on css-tricks, it is cached only for an hour, so you better not use it in production environments.

2) Keep Selectors Simple

Up until recently, retrieving DOM elements with jQuery was a finely choreographed combination of parsing selector strings, JavaScript loops and inbuilt APIs like getElementById(), getElementsByTagName() and getElementsByClassName(). But now, all major browsers support querySelectorAll(), which understands CSS query selectors and brings a significant performance gain.
However, you should still try to optimize the way you retrieve elements. Not to mention that a lot of users still use older browsers that force jQuery into traversing the DOM tree, which is slow.
1$('li[data-selected="true"] a') // Fancy, but slow
2$('li.selected a'// Better
3$('#elem'// Best
Selecting by id is the fastest. If you need to select by class name, prefix it with a tag – $('li.selected'). These optimizations mainly affect older browsers and mobile devices.
Accessing the DOM will always be the slowest part of every JavaScript application, so minimizing it is beneficial. One of the ways to do this, is to cache the results that jQuery gives you. The variable you choose will hold a jQuery object, which you can access later in your script.

1var buttons = $('#navigation a.button');
2
3// Some prefer prefixing their jQuery variables with $:
4var $buttons = $('#navigation a.button');
Another thing worth noting, is that jQuery gives you a large number of additional selectors for convenience, such as :visible, :hidden, :animated and more, which are not valid CSS3 selectors. The result is that if you use them the library cannot utilize querySelectorAll(). To remedy the situation, you can first select the elements you want to work with, and later filter them, like this:
1$('a.button:animated'); // Does not use querySelectorAll()
2$('a.button').filter(':animated');  // Uses it
The results of the above are the same, with the exception that the second example is faster.

3) jQuery Objects as Arrays

The result of running a selector is a jQuery object. However, the library makes it appear as if you are working with an array by defining index elements and a length.
01// Selecting all the navigation buttons:
02var buttons = $('#navigation a.button');
03
04// We can loop though the collection:
05for(var i=0;i<buttons.length;i++){
06    console.log(buttons[i]);    // A DOM element, not a jQuery object
07}
08
09// We can even slice it:
10var firstFour = buttons.slice(0,4);
If performance is what you are after, using a simple for (or a while) loop instead of $.each(), can make your code several times faster.
Checking the length is also the only way to determine whether your collection contains any elements.
1if(buttons){    // This is always true
2    // Do something
3}
4
5if(buttons.length){ // True only if buttons contains elements
6    // Do something
7}

4) The Selector Property

jQuery provides a property which contains the selector that was used to start the chain.
1$('#container li:first-child').selector    // #container li:first-child
2$('#container li').filter(':first-child').selector    // #container li.filter(:first-child)
Although the examples above target the same element, the selectors are quite different. The second one is actually invalid – you can’t use it as the basis of a new jQuery object. It only shows that the filter method was used to narrow down the collection.

5) Create an Empty jQuery Object

Creating a new jQuery object can bring significant overhead. Sometimes, you might need to create an empty object, and fill it in with the add() method later.
1var container = $([]);
2container.add(another_element);
This is also the basis for the quickEach() method that you can use as a faster alternative to the default each().

6) Select a Random Element

As I mentioned above, jQuery adds its own selection filters. As with everything else in the library, you can also create your own. To do this simply add a new function to the $.expr[':'] object. One awesome use case was presented by Waldek Mastykarz on his blog: creating a selector for retrieving a random element. You can see a slightly modified version of his code below:
01(function($){
02    var random = 0;
03
04    $.expr[':'].random = function(a, i, m, r) {
05        if (i == 0) {
06            random = Math.floor(Math.random() * r.length);
07        }
08        return i == random;
09    };
10
11})(jQuery);
12
13// This is how you use it:
14$('li:random').addClass('glow');

7) Use CSS Hooks

The CSS hooks API was introduced to give developers the ability to get and set particular CSS values. Using it, you can hide browser specific implementations and expose a unified interface for accessing particular properties.
01$.cssHooks['borderRadius'] = {
02        get: function(elem, computed, extra){
03            // Depending on the browser, read the value of
04            // -moz-border-radius, -webkit-border-radius or border-radius
05        },
06        set: function(elem, value){
07            // Set the appropriate CSS3 property
08        }
09};
10
11// Use it without worrying which property the browser actually understands:
12$('#rect').css('borderRadius',5);
What is even better, is that people have already built a rich library of supported CSS hooks that you can use for free in your next project.

8) Use Custom Easing Functions

You have probably heard of the jQuery easing plugin by now – it allows you to add effects to your animations. The only shortcoming is that this is another JavaScript file your visitors have to load. Luckily enough, you can simply copy the effect you need from the plugin file, and add it to the jQuery.easing object:
1$.easing.easeInOutQuad = function (x, t, b, c, d) {
2    if ((t/=d/2) < 1) return c/2*t*t + b;
3    return -c/2 * ((--t)*(t-2) - 1) + b;
4};
5
6// To use it:
7$('#elem').animate({width:200},'slow','easeInOutQuad');

9) The $.proxy()

One of the drawbacks to using callback functions in jQuery has always been that when they are executed by a method of the library, the context is set to a different element. For example, if you have this markup:
1<div id="panel" style="display:none">
2    <button>Close</button>
3</div>
And you try to execute this code:
1$('#panel').fadeIn(function(){
2    // this points to #panel
3    $('#panel button').click(function(){
4        // this points to the button
5        $(this).fadeOut();
6    });
7});
You will run into a problem – the button will disappear, not the panel. With $.proxy, you can write it like this:
1$('#panel').fadeIn(function(){
2    // Using $.proxy to bind this:
3
4    $('#panel button').click($.proxy(function(){
5        // this points to #panel
6        $(this).fadeOut();
7    },this));
8});
Which will do what you expect. The $.proxy function takes two arguments – your original function, and a context. It returns a new function in which the value of this is always fixed to the context. You can read more about $.proxy in the docs.

10) Determine the Weight of Your Page

A simple fact: the more content your page has, the more time it takes your browser to render it. You can get a quick count of the number of DOM elements on your page by running this in your console:
1console.log( $('*').length );
The smaller the number, the faster the website is rendered. You can optimize it by removing redundant markup and unnecessary wrapping elements.

11) Turn your Code into a jQuery Plugin

If you invest some time in writing a piece of jQuery code, consider turning it into a plugin. This promotes code reuse, limits dependencies and helps you organize your project’s code base.So that it is easy for people to simply drop them in their sites and use them.
Creating a jQuery plugin couldn’t be easier:
1(function($){
2    $.fn.yourPluginName = function(){
3        // Your code goes here
4        return this;
5    };
6})(jQuery);

1 komentar:

  1. mantap dah artikelnya
    http://essenaquatic.xyz/umpan-ikan-mas-kilo-gebrus/

    BalasHapus