Caching this always a good idea when accessing it several times. It's important to note that this is a JavaScript object - I saw a lot of code that wraps a jQuery object around this for no reason.
Consider this piece of code:
... (function () { alert($(this).attr("class")); });
Compared to much cleaner and slightly faster:
... (function () { alert(this.className); });
Update
In response to your update .. by doing:
... (function () { var that = $(this); that.functionCall(); });
Does not increase performance. This is actually a bit slower since you are creating a variable on top of the this wrapper in the jQuery object.
If you had to work with that - the cached $(this) jQuery object - several times, you will see an increase in performance .. depending on the number of operations:
... (function () // calling a function 1000 times on a cached jQuery object { var that = $(this); for (var i = 0; i <= 1000; i++) { that.functionCall(); } });
On the side of the note: if you are interested in optimizing jQuery performance, there are many great tips in jQuery Tips and Tricks . Give it a go :)
source share