Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8355245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:48:21+00:00 2026-06-09T09:48:21+00:00

These are mainly just some things I’ve been wondering, maybe someone can give me

  • 0

These are mainly just some things I’ve been wondering, maybe someone can give me a little more insight on them, i’ll share what i’ve noticed so far as well!

First thing i’ve been wondering… is there any difference good or reason to use:

$('element').each(function (i, el) { });

— versus —

$.each($('element'), function (i, el) { });

Looking at the jQuery docs I can’t see any rhyme or reason for one or the other (maybe you know an instance or additional things one can do over the other.

But more importantly I’m concerned with speed here

// As opposed to $.each() looping through a jQuery object
// -- 8x faster 
for (var i = 0, $('.whatever').length; i < len; i++) {
    $('.whatever')[i] // do stuff
}

If you check out this jsFiddle DEMO here, you’ll see the difference in speed is basically equivalent with either of them, but more importantly I feel like I should always be using for() loops…

I was just unit testing (looping through each of 5 different scenario functions, 50,000 times), simply looping through a bunch of list items, and setting a data-newAttr, nothing special.


QUESTION :: I guess my biggest question is, why not always use for loops while iterating through an object?? Is there even a point to using $.each()? Do you always use for() loops even when going through jQuery objects?

jsFiddle DEMO here

Function type:                  Execution Time:
_testArea.each() + $(this)               1947   <-- using $(this) slows it down tremendously
$.each()         + $(this)               1940
_testArea.each() + el(plain JS)           458   <-- using the Element speeds things up
$.each()         + el(plain JS)           452
for() loop       + plainJS[0] iteration   236   <-- over 8x faster

Just my 2cents. 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T09:48:22+00:00Added an answer on June 9, 2026 at 9:48 am

    One thing that .each() allows you to do that can’t be done with a for loop is chaining.

    $('.rows').each(function(i, el) {
        // do something with ALL the rows
    }).filter('.even').each(function(i, el) {
        // do something with the even rows
    });
    

    I played around with your JSFiddle to see how chaining would influence performance in cases where you have to loop through subsets of the original set of matched elements.

    The result wasn’t all that unexpected, although I think the overhead of end() was exaggerated here because of the combination of few elements and many loops. Other than that: plain JS loops are still slightly faster, but whether that weighs up to the added readability of .each() (and chaining) is debatable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just trying to ping up some experienced Thread gurus out there...trying to learn more
I've been trying to set up Notepad++ as a little Java environment, mainly for
I'm working on some code that deals with parsing files (mainly XML, but there
I'm trying to reorganize my code and make it more unobstrusive. Mainly, there is
These days I design some algorithms in python, but find first two greatest value
These days, we encountered a strange problem, some of our solr apps on tomcat
I've been playing with an idea for a little program for a while now.
I want to DRY my source code. Let's say I had some of these
I have some code that throws a potential error and I catch these, then
I have RaphaelJS based canvas, where user can do some work. I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.