Performant .live() in jQuery 1.4

So, jQuery 1.4 has very few new features that really make the upgrade worth it (though fixing the .val() on checkboxes and radiobuttons and implementing .detach() are both things that should have been done long ago), but the performance benefits are immense in some cases, so I decided to move the in-progress odkmaker project over.

One significant optimization I’ve been using with jQuery 1.3.2 for some time now was documented by Zach Leatherman — when you use .live() (and if you have a dynamic number of elements you should always be using .live() over .bind()), there’s actually a pretty horrible inefficiency inherent in the way it’s called. Read his post to get the in-depth explanation, but essentially you’re calling into the Sizzle engine to evaluate your selector, when really .live() only cares about the text of the selector. Zach’s method fixes this problem, but unfortunately due to some internal changes in jQuery 1.4, his brilliant method has stopped working.

Thankfully, the fix is pretty simple. The critically missing part is that jQuery objects now have a reference to their context, I suppose to ease use between frames. The resultant code is as follows:

;(function($)
{
    $.live = function(selector, type, callback)
    {
        var obj = $([]);
        obj.selector = selector;
        obj.context = document;
        if (type && callback) {
            obj.live(type, callback);
        }
        return obj;
    };
})(jQuery);

So in the future, instead of

$('a[rel="modal"]').live('click', function(event)
{
    /* some code */
});

you should write

$.live('a[rel="modal"]', 'click', function(event)
{
    /* some code */
});

And thus you save the cost of iterating through the whole document for absolutely no reason.

This technique shaved off up to 7 seconds on pageload on a commercial-grade website I work on, so it would be a huge loss if it stopped working — it’s good news that it’s so easy to fix.

4 Responses to “Performant .live() in jQuery 1.4”


  1. 1 Cerys

    I constantly emailed this web site post
    page to all my associates, because if like to read it after
    that my friends will too.

    Also visit my web page – search engine optimisation *Cerys*

  2. 2 invest property australia

    Hello there I am so excited I found your weblog, I really found
    you by mistake, while I was researching on Google for something else, Anyways
    I am here now and would just like to say kudos for a incredible post and a all round enjoyable blog (I also love the theme/design),
    I don’t have time to read through it all at
    the moment but I have book-marked it and also included your RSS feeds, so
    when I have time I will be back to read a lot more, Please do
    keep up the great job.

  3. 3 website

    We’re always aiming higher. Ayala Land’s Real Estate story started more than 80 years ago –a trajectory for progress, outlining the foundation for the country’s most coveted addresses. Highly regarded growth centers propelled by Ayala Land’s vision of enhancing land, enriching lives for more people. Developed sustainably, communities flourishing for years to come.

  4. 4 heavy rain

    I read this post completely about the resemblance of
    most up-to-date and preceding technologies, it’s awesome
    article.

Leave a Reply