It actually does have a performance impact, even though we can't
always "see it" on modern processors and javascript engines for
desktop browsers. If you consider users with handheld devices that
have functionality similar to PCs (and even take media="screen"
instead of media="handheld" for styling), but have significantly less
processing capacities, then you can see that even as good and clean
code as jQuery core is preforming somewhat lazy. So, a large number of
void requests for selecting nonexistent DOM elements does have a
negative impact.

What's hapenning internaly is basically evaluating document DOM and
matching id '#very-specific-id-not-on-every-page' for every DOM
element (just to conclude that they don't exist and instead of
returning collection of objects, returning an empty collection), and
repeating that for every line with $('something_here'). Then, it goes
a step further and calls the function hover(), just to find out what
to do if it is called on empty object (probaby return void).

So if you do want to keep on with this coding strategy (or want to
avoid rewriting of already written and debugged code), you could at
least find some way of grouping events by functionality or some other
criteria and call only groups that you need, depending on specific
page content. Obvious example: you probably don't need any form
validating events for pages that don't contain forms, or AJAX events
for page with no AJAX at all... Call AJAX events group only for AJAX
pages, form events only for page containing forms and so on. You can
even let the scripts decide what groups to run by declaring which page
goes to which category (could be multiple categories). For example
declare a few group arrays:
var AJAXgroup = new Array("first-ajax-page.html","second-ajax-
page.html","third-ajax-page.html");
$(AJAXgroup).each(function(){ if
( document.location.href.indexOf(this) declare AJAX events  });
Again you will have few loops to run (and every loop impacts
performance), but if you have say 75 events declared and call only 15
that you need, it should be an improvement...

I know answer was too long, but if you got this far it was worth
writing...

Reply via email to