Is it better in practice to make sure the algorithm only initiates the function if there is a need to do so or to just initiate the function in jquery? as an example lets use these pieces of code. imagine a web page has a considerable amount of divs like 1000 or something some of them are nested like a regular web page and how would the programme behave in a $(window).scroll(function(){});
as apposed to a $("element").click(function(){});
. the function fires regardless of weather or not it needs to doesn't it? and might that have performance side effects?
$("div").css({"display":"none"});
or
$("div").each(function(){
if ( $(this).css("display") !== "none" ) {
$(this).css({"display":"none"});
}
});
or even
$("div").each(function(){
if ( $(this).is(":visible") ) {
$(this).css({"display":"none"});
}
});
Please give a detailed explanation on which is better in practice