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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:17:44+00:00 2026-06-11T16:17:44+00:00

I recently came accross a different way of looping though an array in Javascript.

  • 0

I recently came accross a different way of looping though an array in Javascript.

I used to write loops something like this:

for (var len = 0; len < testData.length; len++) {
  total = total + testData[len];
}

I read some code that did it this way:

for (var len = 0; testData[len]; len++) {
  total = total + testData[len];
}

I was wondering how these would perform so I used jsPerf to find it out. The results are pretty amazing. I expected the second method to be a bit it faster than the first, but it’s actually much, much faster.

Is there a down side I’m missing here? Or is this the best way to loop through the items of a list.

Update:

gray state is coming and Diode pointed me to a simple flaw in the testcase which made is seem to be faster.

After correcting the mistake, this is the fastest:

var datalen = testData.length;
for (var len = 0; len <datalen; len++) {
     total = total + testData[len];
}

Update 2:

After testing in some more browsers this testcase takes a different direction again.
Only in Chrome and Opera the normal for loop is the fastest. In other all other browsers Shmiddty‘s way is just a bit faster.

var i = testData.length, sum=0;
while (i--){
    sum += testData[i];
}
  • 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-11T16:17:45+00:00Added an answer on June 11, 2026 at 4:17 pm

    You can also do a loop like so:

    var i = testData.length;
    
    while (i--){
        console.log(testData[i]);
    }
    

    Note that it traverses the array backwards.

    Or, for a summation:

    var i = testData.length, sum=0;
    
    while (i--){
        sum += testData[i];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently came accross some C++ code like the following: if(test_1) if(test_2) { //
I recently came across a way to develop pluggable application modules when using ASP.NET
I recently came across this line in a PHP script: $_REQUEST['start_date']=$date; Is it allowed
I recently came across this article , discussing how to leverage mod_cluster to create
i was recently digging about cookieless sessions, i came accross an article which says
I recently came across this web page http://www.yoda.arachsys.com/csharp/readbinary.html explaining what precautions to take when
I recently came accross a conflict within my team where in a row was
I have been writing C code for many years, but I recently came accross
I recently came across this article which explains how to use HTML5 tags in
I recently came across a Microsoft Interview Question for Software Engineer. Given an array

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.