khs4473 at gmail.com
Tue Mar 13 07:52:39 PDT 2012
BTF is shorthand for "bound-this function expressions". BTFs are
semantically equivalent to classic function expressions with the exception
that "this" is lexically bound.
Using a modified version of the ES parser in UglifyJS, I generated an AST
for the input string. I then traverse the AST, counting all occurrences of
function expressions which do *not* contain a reference to "this" in its
immediate scope. Of those function expressions, I count the number of
functions whose first statement is a return statement, and of those, how
many return an object literal in that return statement.
The results so far can be viewed as a
The tool used for this analysis is available at
The total number of function expressions counted gives us a rough idea of
how many function expressions could be converted to BTFs. In reality the
number could be higher, since I'm not analyzing usage of
Function.prototype.bind to achieve the same goal. For the surveyed code,
the percentage of function expressions which do not reference "this" ranges
between 31% and 87%. About half of all function expressions are suitable
for conversion to BTFs. Clearly BTFs would offer immediate quantifiable
Of the surveyed code, about 19% of BTF candidates contain a return
statement as their first statement. This suggests that BTF syntax should
be optimized for both "expression functions" and multi-statement function
bodies, but with a preference given to multi-statement functions.
Of the "expression functions", only 1.5% return an object literal. This
suggests that BTF syntax should give preference to other forms, if there is
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the es-discuss