Just a small theoretical performance question:
if I have something like:
$(".somediv").each(function() {
// perform some heavy stuff here
});
Would the execution of the code not be faster if I refactored the code of the anonymous function to a named function like :
f = function() {
// perform some heavy stuff here
};
$(".somediv").each(f);
Somehow I have this irrational doubt that tells me that perhaps the anonymous function is re-created each time within the each loop?
If you are concerned about performance, then you should not use
.each(). It is much faster to iterate the contents of a collection with aforloop or awhileloop and have no function call at all than it is to use.each()with its resulting function call for each item.In answer to your question, the anonymous function will not be any slower than the named function. The differences are resolved at parse time before run-time.
This jsPerf shows a plain
forloop as almost 10x faster than.each(): http://jsperf.com/each-vs-for-loop-mine.