Jump to content

omnescient

Members
  • Posts

    5
  • Joined

  • Last visited

Posts posted by omnescient

  1. But it isn't necessarily best practice to assign the length to a variable. It is just the most efficient practice. Sometimes you sacrifice efficiency for readability.

     

    If it was about efficiency, they would also have a chapter about loop unrolling.

    Yes, valid point, although if people are researching node list navigation, they are probably beyond the javascript 'beginner' level. Assigning the length of a node list to a variable before loop criteria are evaluated doesn't seem particularly complicated in that context.

    With regard to loops generally, it is possible to create unintentional infinite loops if the length is evaluated on each iteration, rather than once only before the loop is run, as in the scenario suggested by justsomeguy.

  2. The performance difference is negligible.

     

    It's perfectly OK to put the length property in the loop condition if your application isn't time critical, as is the case with all of the Javascript examples on the site.

     

    It's easier for beginners to understand if you don't add lines of code that aren't relevant to what's being taught.

    Thanks for that Foxy; I didn't see that mentioned on the page to which I referred. However, I'd have thought that encouraging beginners to adopt 'best practice' might itself be regarded as 'best practice' for the tutorials? My post related to consistency of information and advice offered on the site.

  3. for (var i = 0, var l = arr.length; i < l; i++)
    There's actually one circumstance where accessing the length property would be preferable, but it's fairly rare. If the length of the array changes inside the loop then you may want to make sure you're still going through all elements.

     

    In a javascript for loop, I don't believe comma-separated multiple variables each require the 'var' keyword to be stated, just the first.

    I can see what you're saying about the dynamic array length, or in this case the node list length. Perhaps the javascript performance example is too simplistic without an appropriate caveat.

  4. For example, here: http://www.w3schools.com/js/js_performance.aspJavaScript PerformanceReduce Activity in loopsBad Code:
    for(i - 0; i < arr.length; i++)
    Better Code:
    l = arr.length;for(i = 0; i < l; i++)
    The bad code accesses the length property of an array each time the loop is iterated.The better code accesses the length property outside the loop, and makes the loop run faster. But an example here: http://www.w3schools.com/js/js_htmldom_nodelist.asp JavaScript HTML DOM Node ListHTML DOM Node List LengthThe length property is useful when you want to loop through the nodes in a node list:ExampleChange the background color of all <p> elements in a node list:
    var myNodelist = document.getElementsByTagName("p");var i;for (i = 0; i < myNodelist.length; i++) {    myNodelist[i].style.backgroundColor = "red";}
    is inconsistent with that advice.
    // for (var i = 0, j = myNodelist.length; i < j; i++) { ? }
×
×
  • Create New...