Jump to content

Memory/Optimization Question


anon_login_001

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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).

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.