1. #1
    Sencha User
    Join Date
    Jan 2010
    Posts
    28
    Vote Rating
    0
    ladybug_3777 is on a distinguished road

      0  

    Default [Solved] for loop vs .each enumeration

    [Solved] for loop vs .each enumeration


    Wondering which is faster. Using "for" or ".each" to loop through items?

    Code:
    myFormCmp.items.each(function(comp) {//do stuff});
     
    VS
     
    for( var i = 0 ; i <  myFormCmp.items.length ; i++ ){
          var comp = myFormCmp.items.get(i);
          //do stuff
    }
    What is the best practice or does it all depend on what is happening within the loop?
    Last edited by ladybug_3777; 23 Apr 2010 at 9:31 AM. Reason: punctuation

  2. #2
    Sencha User fay's Avatar
    Join Date
    Apr 2007
    Location
    Ireland
    Posts
    1,750
    Vote Rating
    9
    fay will become famous soon enough fay will become famous soon enough

      0  

    Default


    If you look at the source code you'll see that, internally, each uses a for loop; it would be (slightly) faster for you to use "for", but not by a significant amount - it really depends on what you need to do. I use each because it makes my code easier to read!

  3. #3
    Sencha - Community Support Team Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246
    Vote Rating
    89
    Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of

      0  

    Default


    for-loops are faster, but require more lines to code.

  4. #4
    Sencha User
    Join Date
    Jan 2010
    Posts
    28
    Vote Rating
    0
    ladybug_3777 is on a distinguished road

      0  

    Default


    Thanks! Now how do I mark this thread as solved?

  5. #5
    Sencha User fay's Avatar
    Join Date
    Apr 2007
    Location
    Ireland
    Posts
    1,750
    Vote Rating
    9
    fay will become famous soon enough fay will become famous soon enough

      0  

    Default


    Now how do I mark this thread as solved?
    Edit your original post, click "Go Advanced", and add "[SOLVED]" to the post's Title.

Thread Participants: 2