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

  • Home
  • SEARCH
  • 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 9176253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:04:22+00:00 2026-06-17T17:04:22+00:00

http://jsperf.com/jquery-each-vs-for-loop/108 for (var b = a[0], len = a.length; len; b = a[–len]) {

  • 0

http://jsperf.com/jquery-each-vs-for-loop/108

for (var b = a[0], len = a.length; len; b = a[--len]) {
  newArray.push(
    b
  );
}

and

for (var i = 0, len = a.length; i < len; i++) {
  newArray.push(
    a[i]
  );
}
  1. According to jsref, it says the first one is faster. why?
  2. Can anyone explain me the for loop on whats its doing compared to traditional way?
  • 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-17T17:04:23+00:00Added an answer on June 17, 2026 at 5:04 pm

    Your first example just does something very different. Check it out:

    var a = [1,2,3,4],
        newArray = [];
    for (var b = a[0], len = a.length; len; b = a[--len]) {
        newArray.push(b);
    }
    
    > newArray
    [1, 4, 3, 2]
    

    Your second example results in the expected [1, 2, 3, 4].


    If you need to understand the algorithms, it might be easier when converting the for-notation to while-loops and to expand the decrement and increment operators:

    /* first example */
    var b = a[0],
        len = a.length;
    while (len) {
        newArray.push(b);
        len = len-1;
        b = a[len];
    }
    
    /* second example */
    var i = 0,
        len = a.length;
    while (i < len) {
        newArray.push( a[i] );
        i = i+1;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

http://jsperf.com/testing-foreach-vs-for-loop It was my understanding that Test Case 2 should run more slowly than
According to this benchmark http://jsperf.com/function-vs-function created functions run about 1000 times faster. Can you
I created this test http://jsperf.com/loop-counter why is there such a difference between these three
The Benchmark: http://jsperf.com/substringing So, I'm starting up my very first HTML5 browser-based client-side project.
Test here: http://jsperf.com/test-for-speed-of-various-conditionals I'm interested if others are getting the same results, and what
Can someone explain this one to me: http://jsperf.com/string-concatenation-1/2 If you're lazy, I tested A)
Check out this test: http://jsperf.com/delegate-on-vs-bind-5 What I did was delegate the child with a
While investigating some performance issues I stumbled upon the following performance comparison: http://jsperf.com/fastest-canvas-drawing/2 which
I took this test on http://jsperf.com/literal-obj-vs-function-obj and Literal wins on FF6, Opera 10, IE8,
I made some tests to check this but is quite not expected: http://jsperf.com/value-in-array-or-object You

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.