11

Both methods appear to produce the same results, but I've been hard-pressed to actually convince people that the second method works, since it's apparently not commonly known.

// Create some data
var foo = { 'vals':[ {'id':'foo'}, {'id':'bar'} ] }​​​​​​​​​​​​​​​​​;

// Common Method    
$.each(foo.vals, function(i,o){
    alert(this.id);
});

// Alternative (lesser-known?) Method
$(foo.vals).each(function(i,o){
   alert(this.id); 
});

Upon checking the source, these two appear to be one-in-the-same. The second method follows:

each: function( callback, args ) {
  return jQuery.each( this, callback, args );
}

This method demonstrably calls the more commonly-known method, meaning it's just as legitimate. Is this understanding correct, or am I missing something here?

I have typically trusted this method since it didn't cause me to deviate from standard practices with regards to selectors. Let's face it, we're trained to do:

$("p").each();

So it seems only natural to do:

$(obj).each();

Am I mistaken?

4
  • I don't see what the problem is. $(o).each() calls jQuery.each(this, callback, args). What more confirmation/convincing do you need? Feb 23, 2010 at 20:41
  • @voyager: I just want to be sure I didn't have that mistaken. I figured an item like this may be of interest to other jQuery developers, and somebody may be able to add a bit to the distinction.
    – Sampson
    Feb 23, 2010 at 20:42
  • fair enough. It seems that the difference is more of a matter of style rather than functionality. Of course, now you have somewhere to point unconvinced users to. Feb 23, 2010 at 20:44
  • @voyager: Perhaps you should bring that point into an answer ;)
    – Sampson
    Feb 23, 2010 at 20:55

2 Answers 2

13

The difference between the two is actually exponential depending on how you are using it.

The first $.each constitutes a single function call to start the iterator.

The second $(foo.vals).each makes three function calls to start the iterator. The first is to the $() which produces a new jQuery wrapper set (Not sure how many other function calls are made during this process). Then the call to $().each. And finally it makes the internal call to jQuery.each to start the iterator.

In your example, the difference would be negligible to say the least. However, in a nested use scenario, you might find performance becoming an issue.

Finally, Cody Lindley in jQuery Enlightenment does not recommend using $.each for iterations greater than 1000 because of the function calls involved. Use a normal for( var i = 0... loop.

2
  • +1 Excellent information. I was especially happy to hear CL's position on the subject as I suggested for(;;) as an alternative to $.each() recently in another question. Makes me feel smart knowing that CL could have possibly agreed with me ;)
    – Sampson
    Feb 23, 2010 at 21:06
  • What about the reason that $.wach allows you to iterate over any collection while the $().each is only for jquery element ?
    – Royi Namir
    Feb 7, 2013 at 14:22
2

I'm not sure what everybody else says...but I almost exclusively use your second method there.

Coming from .NET land...it just makes more sense to me and makes things easier to read.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.