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
  • 639
  • 5
  • 16

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
  • 188,958
  • 33
  • 188
  • 262