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.

Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145

3 Answers3

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

plalx
  • 42,889
  • 6
  • 74
  • 90
  • 1
    That's right. Basically V8 stores a pointer to the source code. See http://stackoverflow.com/questions/17308446/how-big-are-javascript-function-objects – user123444555621 May 24 '14 at 07:03
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.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • 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 - MONICA WAS MISTREATED May 26 '14 at 18:01
  • @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)`. – jfriend00 May 26 '14 at 18:07
  • By *internal* I mean within the engine, not internally nested. – Qix - MONICA WAS MISTREATED May 26 '14 at 18:08
  • 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 - MONICA WAS MISTREATED May 26 '14 at 18:14
  • @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. – jfriend00 May 26 '14 at 18:23
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.

lrn
  • 64,680
  • 7
  • 105
  • 121