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 9248699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:00:14+00:00 2026-06-18T10:00:14+00:00

I’ve read several questions / articles about this subject and I tested in my

  • 0

I’ve read several questions / articles about this subject and I tested in my solution that the same block of code using for is most of the times faster than each.

However my question is related to the fact than in my page where I’ve around 30 “loops” the starting results using each were around 5300ms (average) with max value of 5900ms and minimum of 4800ms.

And after I’ve changed them to for and the end result surprisingly is slower, takes more time as the previous average (and never got lower than 4800ms and got even higher than 6000ms)….. but when I place console.time('Time') console.timeEnd('Time') in each single “loop block” I get the expected result (FOR is faster).

How is it possible that the global “time” is SLOWER using for than each?

Could please you point me some possible reasons?

P.S.- The full source-code is huge and the important part here is that the only change is: loops each converted to for.

Sample used for the For loop

var l = list.length;  
for (var i=0;i<l; i++) {  

}

Sample used for the Each loop

$.each(list, function (i, item) {

});

Update #1

  • Times are measured using console.time('Time')
    console.timeEnd('Time'). For global time I just use one “counter”. For multiple counters I use different names of course.
  • During all this “process” there are no Ajax requests, so the time different isn’t connected to this.

Update #2

  • As @epascarello requested: the “inner code” of each loop was never changed and it should not be the reason, even more when we access this objects the same way (using For or Each ) list[i].SomeProperty, imho the time differences could never be blamed to the inner code (I think).

Update #3

  • Sometimes I am using Cascading “loops” using different variables i, ii, iii.
  • I often “reuse” variables inside the same function:

var l = list1.length; ...; l = list2.length;

The same applies to the for variables i, ii, iii.

Update #4

I notice a strange behavior in Chrome: the following pattern uses to repeat several times, going down for a while and then spikes up again.

  • DrawGUI: 6159.000ms UP AGAIN
  • DrawGUI: 5990.000ms going down
  • DrawGUI: 5804.000ms going down
  • DrawGUI: 5416.000ms going down
  • DrawGUI: 5315.000ms going down
  • DrawGUI: 5311.000ms going down
  • DrawGUI: 5325.000ms
  • DrawGUI: 5248.000ms going down
  • DrawGUI: 5010.000ms going down
  • DrawGUI: 4886.000ms going down
  • DrawGUI: 5645.000ms *UP AGAIN *
  • DrawGUI: 5247.000ms
  • DrawGUI: 5446.000ms

During all this testes I close all the other chrome tabs and unnecessary applications. Trying to minimize unstable CPU availability.

  • 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-18T10:00:16+00:00Added an answer on June 18, 2026 at 10:00 am

    Since you are benchmarking empty loops you give the JIT (just in time compiler) plenty of room for optimizations. It might be the case that the each call is removed all together as it has no side effects. The for loop however must run as the i is defined in the scope containing the for loop and not inside the for loop.

    (function() {
        for (var i=0; i<10; i++) {}
        console.log(i); // outputs 10
    })();
    

    vs

    (function() {
        [0,1,2,3,4,5,6,7,8,9].forEach(function(i) {});
        console.log(i); // i is not declared
    })();
    

    That is also the reasons why JSLint/JSHint complains about i being declared twice for the following code:

    for (var i=0; i<10; i++) {}
    for (var i=0; i<10; i++) {}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am using JSon response to parse title,date content and thumbnail images and place
For some reason, after submitting a string like this Jack’s Spindle from a text

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.