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();
}
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();
}
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());