10

Does the anonymous function in Foo get re-created in memory each time Foo() gets called?

function Foo(product)
{
    return function(n) {
        return n * product;
    }
}

I'm more or less interested in V8's implementation in particular, since I'm not seeing anything in regards to that in the spec (unless I'm missing something, which I probably am).

I'm kind of confused on the memory management going on since the use of product is specific to the closure that is returned; however, that doesn't necessarily say the inner function has to be re-created along with a closure instance (in theory), since you can still .bind() without losing closure values.

3
  • 1
    What does using .bind() have to do with not losing the closure values. I'm not understanding your point there. Commented May 24, 2014 at 2:25
  • Was just re-iterating that Foo(n).bind() wouldn't change n in the function returned by the call to bind. Commented May 24, 2014 at 2:29
  • @Downvoter, care to explain? Commented Jun 24, 2014 at 1:04

3 Answers 3

5

As far as I know a new function object gets re-created everytime, but the function's code (body) is normally getting reused. I do not know under what circumstances it wouldn't be however.

https://groups.google.com/forum/#!topic/v8-users/BbvL5qFG_uc

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

1 Comment

That's right. Basically V8 stores a pointer to the source code. See stackoverflow.com/questions/17308446/…
4

This code snippet shows that you're getting a new Function object each time:

function Foo(product)
{
    return function(n) {
        return n * product;
    }
}

var a = Foo(2);
var b = Foo(2);

alert(a === b);    // alerts false

Demo: http://jsfiddle.net/wc5Lv/


There are probably interpreter optimizations that can internally reuse the parsed function, but from the pure javascript point of view, a new Function is created each time.

5 Comments

This isn't what I meant. I know the two objects aren't the same. I'm asking if, internally, the return n * product bit is referenced or re-created each time a new function object is returned.
@Qix - My comparison is showing you exactly that. Foo(2) returns the internal function. And my comparison shows that you're getting a different internal Function object each time you call Foo(n).
By internal I mean within the engine, not internally nested.
that seems like an unreliable test at best; and if you read the question I said I was interested mainly in V8 (et al if it was a language spec, which of course it isn't).
@Qix - Why are you beating me up for offering an answer? You've selected your favorite answer so apparently you have the info you wanted. I was offering some other info. Sorry, I didn't remember 2 days later that you had V8 in your original question. By your own explanation here, it's obvious that what you meant by "inner function" in your question isn't very clear. I showed you that the inner Function object is recreated which is what I thought you meant at the time I wrote my answer. It turns out, you meant something else by that. I answered what I thought you meant.
2

Yes. The ECMAScript specification, 5ed, requires that each evaluation of a function expression or function declaration generates a new function object. If you read the cases of http://es5.github.io/#x13 they all contain the phrase "a new Function object"

That just means that there is a new Function object, but most of the internal content of that function object can be shared between instances, including the code generated for the function body. The new object only needs to hold the value of the product variable and a reference to the shared function implementation.

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.