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:

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

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.

10 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

  5. 5 Yahoo

    Excellent post. I was checking constantly this blog and I am impressed!
    Very helpful information specifically the last
    part :) I care for such info much. I was looking for this certain info
    for a very long time. Thank you and best of luck.

  6. 6 rumah murah di cimahi

    When some one searches for his necessary thing, therefore
    he/she wishes to be available that in detail, therefore that
    thing is maintained over here.

  7. 7 inspirasi rumah minimalis

    An impressive share! I have just forwarded this onto
    a friend who was doing a little research on this. And he in fact ordered me dinner because I found it for him…
    lol. So let me reword this…. Thanks for the meal!!
    But yeah, thanks for spending time to talk about this
    matter here on your web page.

  8. 8 This Site

    This particular post is definitely extraordinary; I have found several educational articles on this website. We genuinely wish to thank you men for the good effort. I’ve got book-marked your web site all of which will undoubtedly understand in addition educational and intriguing content throughout upcoming.
    We by now proposed your web site to be able to couple of my pals and could continue to practice it. Alongside educational resource and good ideas We likewise really loved ones excellent blogging skills and become massive supporter of this content writing. Thank you yet again intended for revealing this with us! Many thanks

  9. 9 gambar rumah minimalis

    BTW is there an official confirmation that Not Provided will never impact GWMT data?

  10. 10 grace place berthoud co

    My coder iis trying tо persuade mе to move tⲟ
    .net from PHP. Ӏ Һave always disliked the idea Ƅecause oof thee expenses.
    ᗷut һᥱ’ѕ tryiong none tҺe less.
    Ι’νе beenn սsing Movable-type οn νarious websites ffor ɑbout a үear
    аnd am concerned about switching tο ɑnother platform.
    ӏ Һave heard fantastic things about
    Is there а waay Ι can transfer ɑll
    mу wordpress posts іnto іt?
    Αny ɦelp աould bee greaatly appreciated!

    Herre iѕ mү web site – grace place berthoud co

Leave a Reply