Improve Performance Of This Jquery Script
Solution 1:
One thing I notice is you're doing a lot of unnecessary calls to $()
:
if($(this).hasClass("checked"))
{
$(this).removeClass("checked");
}
elseif(!$(this).hasClass("disabled"))
{
$(this).addClass("checked");
}
else
{
returnfalse;
}
Every time you do $(this)
, there are multiple underlying function calls and a couple of memory allocations involved. It's completely unnecssary. Just do:
var $this = $(this);
...at the beginning, and then:
if($this.hasClass("checked"))
{
$this.removeClass("checked");
}
elseif(!$this.hasClass("disabled"))
{
$this.addClass("checked");
}
else
{
returnfalse;
}
This (no pun) applies to any time you call $()
, not just $(this)
, but you need to be sure you only cache the result for as long as it really will be unchanged (as that varies depending on the selector or element you're passing in). Without reading through with a fine-toothed comb, for instance, you can probably cache the result of $("#filters li.filtersGroup")
in at least one place rather than making jQuery do the whole thing over again.
Solution 2:
Following in T.J. Crowder's Response
You can also clean up your references to $("#filters li.filterGroup")
like this
var$filtersGroup = $("#filters li.filtersGroup");
$("a", $filtersGroup).removeClass("disabled");
$filtersGroup.each(function(index) {
.....
)}
Post a Comment for "Improve Performance Of This Jquery Script"