Is it bad to have too many self-invoking functions in JavaScript?
If they all execute right away (page load) aren’t you taking more resources than if you had regular functions and call them when you need them?
Or is that a bug that exists in my head?
For example:
Thousands of these:
var DoIT = (function () {
//heavy stuff
} ());
VS. Thousands of these:
var DoIT = function () {
//heavy stuff
};
EDIT #1:
I’m talking about things like
- setting alerts
- creating a jQuery dialog with a large form
- re-sizing things when they need to be resized.
Those “self executing” functions usually serve as namespaces. Ideally Javascript written in this manner just returns object that holds other functions to be called as necessary, so execution isn’t expensive. On the other hand if code at hand really is heavy stuff that isn’t always needed, than sure, just don’t call it right away.