Space and time of local variable vs. this

Posted on
  • @Gordon, what's your implementation-knowing 'guess'? ...space / memory footprint, time / performance?

    var obj =
    { property:123
    , funWithThis: function() {
        // function/method using 'this' in body
        // lots of lots this.something(); this.somethingThat(); ...
        return this.property;
      }
    , funWithLocalVariable: function() {
        // function/method using 'var _ that references this' in body
        var _ = this;
        // lots of lots _.something(); _.somethingThat(); ...
      }
      return _;
    };
    
  • You'd have to do some tests...

    It'd be close...

    • this is a reserved word, so accesses will be faster as there's no lookup.
    • But _ is short, and a local variable, so parsing will be faster and lookups shouldn't be that slow either.

    But funWithLocalVariable would probably minify much better.

  • Thanks. ...somewhat as expected. Here two methods of a singleton that use local _-reference for this.:

    ...
    , d: function(e) { var _=this; ((e)?[e]:_.es).forEach(function(e){
       _[e[1]+"D"].apply(_,e); }); return _; }
    , clr: function(cx,i) { var _=this, c; if (cx<0) {
        c=_.clrs[0].apply(_,_.clrs[-cx]); 
      } else { c=cx^((i)?7:0); c=[(c&4)>>2,(c&2)>>1,c&1]; }
      _.lcd.setColor.apply(_.lcd,c); return _; }
    ...
    

    ...that's why I like 'older' virtual/late-late-binding/interpreted languages that use ^ for noisy return... ;-) ...and old new things for the dredging function: => ...and function / block w/ arguments definition: [ :a :b | ...code using local vars a, b ]. ...some 'good old times' that did not need the curly braces and parenthesis salad...

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Space and time of local variable vs. this

Posted by Avatar for allObjects @allObjects

Actions