Question about joined function object of ECMA-262 3rd edition

liorean liorean at
Thu Jul 26 11:19:35 PDT 2007

> Shijun He scripsit:
> > function A() {
> >   function B(x) { return x*x }
> >   return B
> > }
> >
> > var b1 = A();
> > var b2 = A();
> >
> > Spec says: b1 and b2 can be joined, implementation may make b1 and
> > b2 the same object because [[scope]] of them have no difference.
> >
> > Two call of A() will produce a function from the same FunctionBody, so
> > they are equated and the result function objects can be joined, am I
> > right?

On 26/07/07, John Cowan <cowan at> wrote:
> It is not because b1 and b2 come from the same FunctionBody, but because
> the function B has no persistent state, that they can be joined (which
> is just a way of saying they are the same object).  Because A has no
> local variables, B doesn't have to care that it was nested inside A.

A could have any number of local variables, b1 and b2 could still be
joined. The important factor is that it doesn't use variables from the
containing scope(s), so there is no observable difference.

> Shijun He scripsit:
> > function C(x) {
> >   function D() { return x*x }
> >   return D
> > }
> >
> > var d1 = C(1);
> > var d2 = C(2);
> >
> > Are these two call of A() also are equated uses of the same source?

On 26/07/07, John Cowan <cowan at> wrote:
> No.  In this case, d1 and d2 have to be different objects, because they
> have different persistent states:  within d1, the variable x (which is
> free in D) is bound to 1, but within d2, the variable x is bound to 2.
> Thus d1 and d2 must be different objects.

Only because they actually use the variable x, however. They don't
have to have identical scope to be joined, but they have to be
outwardly indistinguishable. Any nested function which only uses
variables from it's own variable object can be joined. It's also
possible to join function objects in some cases where they do use
variables other than from their own variable object, given that all
variables used are guaranteed to be the same - while doing this
joining is a memory footprint optimisation, it will make for more
complex (i.e. slower) compilers however.

On 26/07/07, John Cowan <cowan at> wrote:
> In the A-B case, it is not a requirement that b1 and b2 are the same
> object, but interpreters are permitted to make this optimization to
> save on allocation.  Whether any of them actually do the optimization
> is another question.  It may be more trouble than it's worth.  However,
> a proper ES compiler would probably want to do so.

If any implementation would chose to do the joined objects behaviour,
are they not more likely to actually just use a reference to the same
function object instead of joining two different objects? Joining
different function objects doesn't make sense if you can actually use
the same object - after all, each removal or addition of a property on
a joined function object will take extra time depending on how many
other function objects it's joined to, each creation of a joined
function object will require one enumeration of all properties of the
original function object, and each joined function object will take up

However, I think the compiler complexity added for implementing this
will give a negliable reduction in footprint, so most implementors
haven't found it worthwile to do it.
David "liorean" Andersson

More information about the Es4-discuss mailing list