Reputation: 31
Is there any memory usage difference between anonymous functions and normal functions in Javascript ?
If so, how? Can you explain it?
Upvotes: 3
Views: 164
Reputation: 382150
If by "normal functions" you mean functions declared as function a(){
at the root level of your script, that is functions attached to the window object, yes there are differences :
But those differences are usually minor, you normally don't have to pay attention to that. In most of your pages your anonymous functions wouldn't be garbaged anyway as you don't remove the event handler (the first root cause of anonymous functions usually).
Globally (premature optimization is the root etc.) I recommend you favor readability as long as you don't have garbaging problems. Javascript engines and their GC change a lot, so your efforts could be useless.
Google has an interesting notice about closure and memory.
Upvotes: 4