javascript - Searching the DOM on click events -
say have following code:
$('.some-class a').click(function() { // }); will jquery search dom each time .some-class a clicked? or dom searched once? i'm wondering if improve performance if make $('.some-class a') variable , change to:
var $someclass = $('.some-class a'); $someclass.click(function() { // });
in case :
$('.some-class a').click(function() { // }); this instruction executed once : jquery search dom once, find requested elements , attach click events them. end.
in case :
function hidestuff(){ $('.some-class a').hide() } jquery have search dom every time function called. in order avoid that, store objects in variable (it's called caching) :
var $elems = $('.some-class a') function hidestuff(){ $elems.hide() } every time hidestuff() function called, jquery won't have search dom, it's got elements stored in memory, it's faster.
Comments
Post a Comment