As always, you would need to run a test to really know which is faster.
Create a large number of elements and try both versions.

My gut sense is that method 1 will be faster. Running the three .each()
loops won't be much different than running the single .each() loop over all
the elements - take a look at the source code for .each() and you'll see
that there isn't much setup overhead, and you are looping over the same
number of elements either way. (I assume the presence of .someClass4 in
method 2 but not method 1 is just a typo.)

However, method 2 has all the extra overhead of making *three* .hasClass()
calls on each element. That's a significant amount of extra overhead.

Method 1 is also cleaner and simpler, and since it's likely to be faster,
it's what I would use.

-Mike

> From: alex.allah
> 
> Method #1
> 
> $('.someClass1').each(function(){
>       // Some stuff here
> });
> 
> $('.someClass2').each(function(){
>       // Some stuff here
> });
> 
> $('.someClass3').each(function(){
>       // Some stuff here
> });
> 
> Method #2
> 
> $('.someClass1, .someClass2, .someClass3, 
> .someClass4').each(function() {
> 
>       var el = $(this);
> 
>       if (el.hasClass('someClass1'))
>       {
>               // Some stuff here
>       }
> 
>       if (el.hasClass('someClass2'))
>       {
>               // Some stuff here
>       }
> 
>       if (el.hasClass('someClass3'))
>       {
>               // Some stuff here
>       }
> });
> 
> 
> Which method is faster and better?
> 

Reply via email to