BTF Measurements

John J Barton johnjbarton at johnjbarton.com
Tue Mar 13 09:09:09 PDT 2012


On Tue, Mar 13, 2012 at 8:15 AM, Jason Orendorff
<jason.orendorff at gmail.com> wrote:
> Kevin Smith wrote:
>> 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
>> benefit.

I'm confused by this paragraph. Functions that benefit from 'this'
binding would reference 'this' or an alias for 'this' right?

...
> This is great data to have.
>
> Something I found really interesting about this is that if you delete
> Facebook and Netflix, this number goes up to 27%. In the MooTools
> code, it's 40%. So either it just varies wildly depending on
> programming style, or else library code has a greater need for
> "expression functions" than application code.

Everyone has to deal with the this-binding problem; every project will
have some strategy. So a better analysis would start by determining
the strategy for each project, then using the counting analysis to
determine the ratio of functions needing this-binding based on the
strategy. I think you will come up with a much higher number.

Some of the this-binding strategies I know about:
  var self = this; // usually 'self' but certainly 'this' alone on ther RHS
  Prototype bind http://www.prototypejs.org/api/function/bind
  local library bind/bindFixed (eg Firebug source)
  function() {}.bind(this); // modern code

jjb


More information about the es-discuss mailing list