Are there any drawbacks to using anonymous functions in JavaScript? E.g. memory use?

All JavaScript functions will behave in the same manner in that they inherit the variable environments in entire scope chain leading up to, and including, themselves. This is equally true for both anonymous and named functions.

All JavaScript functions will behave in the same manner in that they inherit the variable environments in entire scope chain leading up to, and including, themselves. This is equally true for both anonymous and named functions. This chain of references to the outer environments stays with each function, even if the function is passed into an entirely different scope.

Traditionally, this would mean that all variables in any given chain have a reference retained to them as long as the inner closure continues to exist. Although in modern browsers that compile the code, it is likely that there will be an analysis of which variables are actually referenced, and only those will be retained, allowing others that are no longer referenced to be garbage collected. However, there are other situations where an anonymous function is wasteful.

Here's a common bit of code: for( var I = 0; I Log( j ); }, 1000 ); } for( var I = 0; I I'd say that it is good to avoid this if possible but not to be obsessive about it. Sometimes a new variable environment added to the scope chain is simply the best solution. EDIT: Here's an article from Google.

Specifically, see Avoiding pitfalls with closures. For information on the performance impact of extending the scope chain, and for a claim that anonymous functions are “slower” than named functions.

1 Moderator Note Comments under this answer were removed because they degenerated into more noise than signal. Please keep comments constructive, professional and most of all on topic. – Tim Post?

Jul 16 '11 at 18:08 1 Thanks to @Tim Post for the removal. The result of the relevant discussion with OP, as well as the performance test provided by @Felix Kling has been added to the answer. – user113716 Jul 16 '11 at 18:12.

I think what you probably read about was the IE closure memory leak problem. Look at this article on the issue. Basically, on older versions of IE, the garbage collector could not handle circular references involving DOM objects.

Since closures are very conducive to such circular references, they could easily lead to annoying memory leaks in IE.

Yeah, I’m not sure it was that — I do remember the IE circular references thing, and that it had to do with closures (I could never quite get the full details of it to stick in my head), but I think the thing I read was a more general point about anonymous functions. – Paul D. Waite Jul 14 '11 at 18:54.

This is true, due to closures that are created. As a general rule the biggest issue with the practice is performance concerns with IE (especially older versions of IE), which has a terrible track record of dealing with these properly.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions