If you keep your Javascript code in a single file it only has to be loaded once (and perhaps even compiled once, see this), when a user requests their first page of your website. Then the browser will use a cached copy unless you disallow caching (bad configuration of your web server can do that).
When you do something like (JQuery):
$('#nosuchid').something();
or e.g.
$('.nosuchclass').each(function(i, el) { ... });
there will be no error and no effect if no matching elements exist in your markup. If you use, say, document.getElementById()
, it will return null
if no elements match and you will have to handle that case. If you do that properly there aren't going to be any errors.
As you can imagine, the code which is skipped doesn't take time to execute, so there's no reason to expect performance problems, either.