3 jQuery Nuggets

3 jQuery Nuggets

In this post, I will present 3 tiny jQuery plugins. They are present in almost all of my jQuery adorned pages. At least theoretically this means they might be worth Your time.

3 nuggets
3 nuggets


I call these plugins “nuggets”, because they are so small but very precious. They can save You a lot of unhappy jQuery debugging time.

Flush

With jQuery it is very easy to append/prepend/clone any amount of complex html, and in the process create in memory a lot of dom nodes, and thus to confuse the browser garbage collector immensely.

Above code is really bad jQuery code, and I do sincerely hope very rarely seen(?). But. You never know when you will notice your shiny and complex jQuery based page, is starting to cause mysterious 100+ MB RAM consumption. For this kind of situations, this “nugget” flush() plugin is a real jQuery DOM leaks “basher”. This “nugget” has made me (almost) completely forget about the bad old days when we wondered to no end, why is one page becoming so unresponsive in IE, or why is memory consumption of a browser rising so dramatically, when hosting some complex jQuery based page. So, here it is:

While developing any more complex page, it is very easy to slide into the “DOM leaks” black hole. This is mainly because inevitably in such pages, we do a lot of, so called “DOM manipulations”. Usually xml/json message comes from a server side, we make the html markup from it, we then perhaps use jQuery to remove some div contents, create new dom elements attach them inside the div “container”, then proudly style and display the result. And so on, as user works on this same page, until user does not leave to the next page.

Especially the DOM element removal is highly debatable. From the W3C spec, where it was somewhat naively designed, all the way to modern jQuery based implementations. Ditto … DOM element remove() and browsers are not very good friends.Yes, modern HTML5 browsers too. Especially when JavaScript is mixed-in. The key difficulty, browsers are facing, is to know when is an DOM element not in use any more? Here I do not mean “detached”, I mean “not in use”. That is: When any given DOM node can be actually removed from memory. And of course poor old IE, is (was?) the worst one in this game. The Usage:

Unbindall

But, still this is not a complete jQuery and dom leaks solution. There are DOM events too. jQuery handles events very effectively. One can get carried away easily and forget to “dispose of event objects safely” when finished. This is and is always going to be a source of lot of grief, and not just for begginners. Forgeting to relase events is also a cause of thousands “calls for help” on numerous jQuery forums. And even angry jQuery “bashings” in forums, if calls are not promptly answered. For my pages, I have solved the problem of not released events with this following tiny plugin.

The key issue with event releasing is “where to do it”. Not how to do it. Without detouring into that subject, I will just offer this solution:

The above can be a “real life saver” for IE hosted jQuery enabled pages with any ammount of feverish jQuery DOM event binding.

T Junction

And now, and at last a tiny plugin that is not invented for combating DOM leaks. This one is for allowing long and uninterrupted and happy jQuery chaining. A favorite sport of many “jQueryans”.

What is going on here? The above plugin takes callback and whatever you need and gives it to the callback as its argument. And then simply returns the jQuery current instance, allowing for the chain to proceed.
If called without arguments T$ will “do nothing”. Ok, but why is this usefull? Here is why: $T() plugin gives you an simple chain interrupt-and-proceed opportunity. In your beloved and long jQuery chains. Consider this, very frequent jQuery pattern:

What happens above if selector “whatever” produces empty jQuery instance? Above, can be a very complex find(). We may want, for example, to log the result of a find, or do something if one thing is found but not the other, or anything else, before chain goes into potentially lengthy jQuery stack of elements, to be used by each(). Without $T() aka “T junction” , we would need to do something like this.

With the help of $T(), we have much cleaner and uninterrupted chain.

Above is (by the way) nice, useful and eminently reusable mechanism of using jQuery.each(), built on top of this tiny $T() plugin.
Obviously inside the callback argument given tor $T(), we will want finish as quickly as possible, in real life situations, so that the whole chain is not slowed down, inside the T$() call.

This is it. 3 tiny and precious jQuery plugins for Your jQuery code. Not a lot of code, just a lot of fun :)

NOTE: This post was originally posted 2009 Oct 02.

5 thoughts on “3 jQuery Nuggets”

Comments are closed.