Which is better performance wise.
foo(this);
function foo(element) {
$(element).index();
}
Or should I do
foo($(this));
function foo($element) {
$element.index();
}
Obviously taking into account that I will be using the argument a fair few times inside the function.
Thanks!
Connor
It doesn’t matter where you wrap an object on jQuery if you’re going to wrap it anyway.
It only matters that you cache the wrapping result and don’t wrap it twice.
For that matter the following rules apply to many plugins’ code:
1) jQuery vars are all prefixed with $:
var $this = $(this)2) never wrap $-prefixed var in $
3) always cache (save to var) any jQuery-wrapped expression used more than once
4) if the same wrapped object (like
var $items = $('ul li');) occurs more than once in several similar functions, move it to the outer scope and rely on closure.