0

Do I save any memory or performance if I do this

function foo(){
   var $this = $(this);
   var class = $this.class();
   var attr = $this.attr();
}

Over this

function foo(){
   var class = $(this).class();
   var attr = $(this).attr();
}
user3822370
  • 641
  • 7
  • 20

1 Answers1

1

The performance difference may be minimal for simple applications, but it exists. Consider that $() is a function, so any time you invoke that function the engine has to execute the code within that function. Referencing an existing variable is going to be a lot faster than executing that potentially large function.

To put it another way, this:

var x = someFunction();
someOtherFunction(x);

will always be faster than this:

someFunction();
someOtherFunction(someFunction());
David
  • 208,112
  • 36
  • 198
  • 279