0

Say I have a function like this:

function wrap_function(fnInput)
{
    if (somecondition)
    {
        return function() { 

            // Simplified example, in reality doing more stuff in here
            fnInput.apply(this, arguments)

        }
    }
    else
    {
        return fnInput;
    }
}

I'm assuming that if somecondition is false this function won't create a closure and therefore won't have performance/memory impacts associated with closures.

Is this correct?

1
  • The "performance/memory impacts" of a closure in this case is absolutely minimal. If somecondition is true, the activation object created when wrap_function is called stays on the scope chain of the anonymous function, but might be optimised away since no use is made of it. fnInput might be a direct reference to the original (function?) object passed to wrap_function. In the worst case, there is only one extra lookup when resolving the identifier fnInput inside the anonymous function. Commented Aug 24, 2011 at 1:23

1 Answer 1

1

If somecondition is false a closure will not be created. You're using the loosely typed nature of javascript in your example

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

1 Comment

Thanks... that's what I thought, just needed to be sure.

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.