SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Variable declaration and loops

    This might be a stupid question, but it won't leave me until I ask, so here it goes:

    Is there any difference (either conceptually or in relation to performance) between declaring a variable before or inside a loop, when that variable is going to take a different value for each iteration? For example:

    Code:
    var someElement;
    for (var i=0 ; i < aCertainArray.length ; i++) {
       someElement = aCertainArray[i];
    }
    
    or
    
    for (var i=0 ; i < aCertainArray.length ; i++) {
       var someElement = aCertainArray[i];
    }
    Actually, let's expand the question: is it worth avoiding the someElement variable altogether, and using aCertainArray[i] instead?

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is it worth avoiding the someElement variable altogether, and using aCertainArray[i] instead
    It depends- if you only refer to aCertainArray[i] once, you don't need the other variable.
    But every lookup is a hit, so if you need to look it up more than once, use the variable.

    If you need to maintain the item in the array and the function changes its value, use the variable.

    There is a tiny improvement in performance if you declare the variable, if you need it, only once, before the loop.

    But you will save the most if you calculate the length of the array once, instead of on every iteration:

    Code:
    var someElement;
    var L=aCertainArray.length;
    for (var i=0 ; i < L; i++) {
       someElement = aCertainArray[i];
    }

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks mrhoo.

    Quote Originally Posted by mrhoo View Post
    There is a tiny improvement in performance if you declare the variable, if you need it, only once, before the loop.

    But you will save the most if you calculate the length of the array once, instead of on every iteration:
    So you're saying that declaring in before the loop doesn't really matter, and if performance is an issue, the thing to consider is calculating the length of the array once, is that right?

    I had heard about calculating the length only once, is it worth doing? Or only for very big loops? My feeling is that none of this would have a big impact in performance, but I've never done any testing or read any specific material about it.

    Just to clarify, this is all theoretical talk, I'm not having performance issues. But I love this kind of question. And it's interesting to see what other programmers think about them.

  4. #4
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, it is faster. Try this testcase
    Code:
    var aData =  [];
    
    // Populate array
    for(var i=0; i<200000; i++) {
    	aData[i] = ["28.02.2004", "Jag och Lars har samma m&#229;l - vi ska till EM", "1190 kb", "dummy data"]
    }
    
    var d1 = new Date();
    
    for(var i=0; i<aData.length; i++) {
    	aData instanceof Array
    }
    
    var d2 = new Date();
    
    
    var d3 = new Date();
    
    for(var i=0, iLength = aData.length; i<iLength; i++) {
    	aData instanceof Array
    }
    
    var d4 = new Date();
    
    alert("First iteration took " + (d2-d1)/1000 + " seconds");
    alert("Second iteration took " + (d4-d3)/1000 + " seconds");
    You will see that it will be faster if you define the array length in a variable. You will see even bigger difference in speed when you are iterating through a node collection.

  5. #5
    SitePoint Member
    Join Date
    Nov 2006
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The var keyword is necessary, try this.
    First type in:
    Code:
    var myString;
    Then type in:
    Code:
    myString;
    It should throw an error. And also, what those guys said .
    If you declare a variable once when you change it's value you don't need the var keyword.

  6. #6
    SitePoint Enthusiast
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pepejeria View Post
    Yes, it is faster. Try this testcase
    ...
    I tried it. FF and Opera (Win) both gave different results, Opera sensibly faster. IE freezed (?).

    In both cases (FF and Opera), the results were not stable: sometimes both loops took exactly the same time, somtimes the second one was faster. But even then, the difference is *very small* (around 0.01s). This is for 200000 iterations, which makes me think that for a standard loop the performance impact would be negligible.

    Still I like: for (var i= 0, iLength = arr.length; ...
    So I'll probably start coding my loops like that. Not for performance, but for conceptual beauty...

  7. #7
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, you are right, for small Arrays of data it doesn't matter. But as I said, the speed difference will be more noticeable when you are iterating a node collection. Since it then need to check the DOM each time it iterates.

    Opera is the fastest browser no doubt when it comes to JavaScript/DOM speed. IE being, no surprise, dumb slow.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •