| Author |
Checking for postive length of jQuery wrapped set before calling methods on set
|
Michael Matola
whippersnapper
Ranch Hand
Joined: Mar 25, 2001
Posts: 1721
|
|
I've come across some jQuery code that checks the length of the wrapped set before performing certain "getting rid of" methods, such as hide() or remove(). Presumably, the coder's intent is only to call hide() or remove() if there's actually something matched to hide or remove.
Elsewhere in the same code (presumably written by the same person, or team), there's no length check before other operations:
Are such length checks necessary? If so, why?
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56183
|
|
Not necessary. If there's nothing in the set, the methods will be a noop.
Building the wrapped set twice is also an unnecessary performance hit.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Michael Matola
whippersnapper
Ranch Hand
Joined: Mar 25, 2001
Posts: 1721
|
|
Bear Bibeault wrote:Not necessary. If there's nothing in the set, the methods will be a noop.
That's what I was thinking. I wondered if it stemmed from a fear of calling a method on a null, but a set is returned, albeit empty.
Bear Bibeault wrote:Building the wrapped set twice is also an unnecessary performance hit.
Yes! In this example, I'm guessing the set is not a very expensive one to build, but in the general case, why would you do it twice.
Thanks for the confirmation.
Mike "off to fight crime" Matola
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56183
|
|
Michael Matola wrote:Mike "off to fight crime" Matola
What color is your cape?
|
 |
Michael Matola
whippersnapper
Ranch Hand
Joined: Mar 25, 2001
Posts: 1721
|
|
|
Invisible.
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56183
|
|
Ah, The Stealth Superhero.
|
 |
 |
|
|
subject: Checking for postive length of jQuery wrapped set before calling methods on set
|
|
|