anon_login_001 Posted July 9, 2008 Share Posted July 9, 2008 I'm using the Mootools framework, in case this code looks a bit strange. Nothing is broken. My question concerns event handlers and memory usage: When implementing a dispatcher/handler for an event, is it more effecient to do something like this: $(document).addEvent('keydown', worksheetKeydownDelegator() } ); Instead of using anon' functions, like this: $(document).addEvent('keydown', function(ev){ worksheetKeydownDelegator(new Event(ev)) } ); ? Just wondering if, for instance, attaching anonymous functions was creating a new function for each element, and if it made any difference from using a function name directly. Quote Link to comment Share on other sites More sharing options...
Third_Degree Posted July 10, 2008 Share Posted July 10, 2008 I'm guessing more code = more time, but this is not ALWAYS the case. Time it? Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted July 10, 2008 Share Posted July 10, 2008 Functions are objects and each object has memory associated with it. In the case of a predefined function (including ones you wrote yourself), the function (i.e. the object) exists only once and is referenced by the named attached to it. When you assign additional references to this object, all you are doing is creating memory for a new reference and NOT a new object. In the case of anonymous functions, I believe they are instantiated on the spot and then the reference points at the newly created object (i.e. the function). This means that a piece of code that operates in this manner that is repeatedly called will create many, many objects. I could be wrong, but you could test this easily enough. Create a loop that assigns a predefined event handler to 100,000 DOM elements and then assign a single lambda event handler to 100,000 DOM elements. Watch the memory usage of the browser during this process. Quote Link to comment Share on other sites More sharing options...
anon_login_001 Posted July 10, 2008 Author Share Posted July 10, 2008 That's what I was thinking... I'll try and benchmark it myself, and 'unofficially' report my results. Thanks for the input! Quote Link to comment Share on other sites More sharing options...
anon_login_001 Posted July 10, 2008 Author Share Posted July 10, 2008 You know, I realized I pasted in the wrong code examples to begin with.... My question really did concern which style function would have been better, being attached as an event handler for hundreds of inputs/objects, not just the document object. Silly me. From a couple basic observations of just 5,000 text inputs, I saw no substantial difference in memory. [ "banchmarks" on a PC that I'm currently working on... this was not a sanitary benchmark environment! ] I realize, however, that attaching all those events is just plain 'the wrong way' to go about it anyway, as it locks up the browser during garbage collection (leave the page, close the window, etc). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.