0

I have a javascript function which contains another javascript function inside (closure)

function function1() {
    $("button").bind("click", function () {
        function2();
    });

    function function2() {
        // code
    };
};

My question:

When i call function1() for many times, does the function2() gets created each time (and saved in memory)? or it is shared?

function1 is not used as a constructor, so i don't think i should use prototype

5
  • 1
    yes the function is created every time but gets destroyed after execution of the function1() hence not present in the memory after execution of the function1. I would use firebug to check the same Commented Oct 8, 2013 at 15:10
  • How can i check this with firebug? Commented Oct 8, 2013 at 18:03
  • it's a firefox plugin. Just google it. You'll find it Commented Oct 8, 2013 at 18:36
  • I do use firebug a lot, but didn't knew how to check for js memory. I think you talk about firebug profiler Commented Oct 9, 2013 at 6:07
  • 1
    @AkshayKhandelwal no the function will not disappear. It will only disappear when the jQuery event handler is cleared (implicitly removes the $.cache reference to the anonomyous function which in turn keeps alive function2 from closure). After function1 returns, the unique function2 that was just created is easily available even to you as user from window.$.cache[xx].events.click[0].handler so destroying it would be pretty dangerous :) Commented Oct 9, 2013 at 7:43

2 Answers 2

2

Each time you call function1, a new function2 is created and saved in memory, and is signed up as part of a click handler.

The function2's that are created by function1 can't get garbage collected as long as they could potentially be called through your click handler.

Sign up to request clarification or add additional context in comments.

4 Comments

Removing the element from DOM using $.remove() will allow function2 to be garbage collected?
I don't know whether $.remove() allows function2 to be garbage collected in browser implementations. I would expect it to, because otherwise it would be a memory leak.
@RaraituL: Yes, removing the element from DOM will cause function2 to be garbage collected in all browsers. In IE versions less than 7 function2 will not be garbage collected if it contains references to the element since IE7 and below had a bug that couldn't garbage collect circular references if those references lead to the DOM. Regular, non-DOM circular references are garbage collectible. Most libraries like YUI and jQuery has functions that recursively removes event handlers on DOM elements for exactly this reason.
@RaraituL: Hmm.. reading from the docs: api.jquery.com/remove. It looks like jQuery .remove() unbinds all event handlers recursively before removing the element from DOM. So jQuery already handles that for you. So the answer is yes, jQuery's remove() will make function2 be garbage collected.
0

Yes, function2 would be created each time function1 is executed, which is possibly an avoidable inefficiency.

The code in the question would execute more efficiently as follows :

function function2() {
    // code
};
function function1() {
    $("button").bind("click", function2);
};

Thus, function2 is defined once and used, potentially, many times over.

The price you pay for this efficiency is to deny function2 the opportunity of accessing any vars declared inside function1. As given, no such vars exist, so you would be OK.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.