Don't Act on Absent Elements
Prev Chapter 9. Performance Best Practices Next

Don't Act on Absent Elements

jQuery won't tell you if you're trying to run a whole lot of code on an empty selection — it will proceed as though nothing's wrong. It's up to you to verify that your selection contains some elements.

// BAD: this runs three functions
// before it realizes there's nothing
// in the selection
$('#nosuchthing').slideUp();

// Better
var $mySelection = $('#nosuchthing');
if ($mySelection.length) { mySelection.slideUp(); }

// BEST: add a doOnce plugin
jQuery.fn.doOnce = function(func){ 
    this.length && func.apply(this);
    return this; 

}


$('li.cartitems').doOnce(function(){
    
    // make it ajax! \o/

});

This guidance is especially applicable for jQuery UI widgets, which have a lot of overhead even when the selection doesn't contain elements.


Copyright Rebecca Murphey, released under the Creative Commons Attribution-Share Alike 3.0 United States license.


Prev Up Next
Use $.data Instead of $.fn.data Home Variable Definition