aspose file tools
The moose likes HTML, CSS and JavaScript and the fly likes Checking for postive length of jQuery wrapped set before calling methods on set Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Reply Bookmark "Checking for postive length of jQuery wrapped set before calling methods on set" Watch "Checking for postive length of jQuery wrapped set before calling methods on set" New topic
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
    
  13

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
    
  13

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
    
  13

Ah, The Stealth Superhero.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Checking for postive length of jQuery wrapped set before calling methods on set
 
Similar Threads
shrinking a div vertically
Retrieve id element by index
"String" and == with methods?
Bug in code, need an outside perspective - cannot figure it out.
jquery