SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    possibly esoteric ? about javascript variables in simply js

    here's a question about js performance:

    in a function that loops several times/second, is there a performance hit when a local variable is repeatedly declared as such? i.e.

    Code JavaScript:
    var blah = Object.doingStuff + ObjectFriend.doingOtherStuff;

    assume that changes to that variable are happening in the function, and that new values in Object & ObjectFriend affect it.

    does the environment think that 'blah' is a new variable *every* iteration? how could that be optimized if so?


    just curious and i figured if someone answers it, it will help others. (i thought about it looking at such a loop in the animation chapter in simply javascript, e.g. expandAnimate p. 204)

  2. #2
    SitePoint Guru Chroniclemaster1's Avatar
    Join Date
    Jun 2007
    Location
    San Diego, CA
    Posts
    784
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not a JS guru, but I believe that loops are absolutely stupid. Unless there's some subroutine checking and remembering (and that would probably cause even more of a performance hit), the loop absolutely calculates the "blah" in your example EVERY time through. That's why loops are such a major part of performance enhancement, because the more times you loop through a piece of code, the bigger the performance savings if you can optimize that code block. Moving "blah" out of the loop is a typical way to optimize your performance, unless blah is changing or otherwise HAS to be calculated in the loop.
    Whatever you can do or dream you can, begin it.
    Boldness has genius, power and magic in it. Begin it now.

    Chroniclemaster1, Founder of Earth Chronicle
    A Growing History of our Planet, by our Planet, for our Planet.

  3. #3
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks, that answers the question. the loop that made me think of it fits the criteria of your last sentence: blah is changing *and* has to be calculated to make the loop function.

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,607
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Whatever values don't change inside the loop should be calculated once before the start of the loop. The only calculations inside the loop should be those that can change with each time through.

    Most people don't seem to worry about it though as many loops are defined as

    for (i=0; i < array.length; i++) {...}

    and since most of them are not changing the length of the array during the loop the test for its length should be done once and that constant length tested each time through.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks felgall.
    my question was specifically about whether JS operated differently than other languages in the loop b/c of an example function i read in a book.

    within the function a local variable was declared using 'var', and i wondered whether using 'var' was increasing overhead as opposed to declaring the var outside the loop and just having that variable re-evaluated w/in the loop w/o a 'var' declaration. it would be one more line of code, certainly not DRY, but the performance gain might be worth it. hence the specific, esoteric, question about JS declarations. i should have been more thoughtful about my wording of the question!

    i didn't specify more b/c i'd originally posted to the questions forum re: simply javascript.(b/c that's where i saw the code and i referenced the code & page in the book) and some moderator (i presume), wisely put the post in this forum.

    from other languages i've worked in i'm aware that declaring variables repeatedly in loops is a bad idea. i just wondered if JS was different. sorry if i'm rambling...2 much coffee...
    :-)

    i appreciate the comments!

  6. #6
    Caveat surfer Buddy Bradley's Avatar
    Join Date
    May 2003
    Location
    Cambridge, UK
    Posts
    2,366
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So your question is really which of these is more efficient/better:
    Code:
    for (i=0; i<10; i++) {
      var x = i * 10;
      doSomethingWith(x);
    }
    Code:
    var x;
    for (i=0; i<10; i++) {
      x = i * 10;
      doSomethingWith(x);
    }
    I'd guess that there may be a tiny performance hit from re-initialising the variable each time, but no memory implications, but I don't really know.

  7. #7
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, that's a great and concise example of what i mean. i wondered, if that loop were running 100s of times, how much does the re-initialization cost?

    i just started the definitive guide (o'reilly). maybe i'll find a 'definitive' answer in there. ;-)

    if i do, i'll post it here in case anyone cares. :-)

  8. #8
    SitePoint Guru Chroniclemaster1's Avatar
    Join Date
    Jun 2007
    Location
    San Diego, CA
    Posts
    784
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I care! I find O'Reilly books good for the really technical info. Hopefully they have something. But I agree with Buddy, there will be a "small" performance boost. I'm not sure exactly how much, not enough to really notice any difference in the apps I've worked on in JS or any other language.
    Whatever you can do or dream you can, begin it.
    Boldness has genius, power and magic in it. Begin it now.

    Chroniclemaster1, Founder of Earth Chronicle
    A Growing History of our Planet, by our Planet, for our Planet.

  9. #9
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    as promised, here's what i found in the rhino book:

    p. 51 (5th ed)
    "4.2.1 Repeated and Omitted Declarations

    It is legal and harmless to declare a variable more than once with the var statement. If the repeated declaration has an initializer, it acts as if it were simply an assignment."

    almost exactly what i was looking for.

  10. #10
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The function of var is to specify the scope of the variable - not to create a new variable (although that is the consequence). I would think that repeated use makes no difference, whatsoever. Interesting thought, though.

  11. #11
    SitePoint Member
    Join Date
    Jul 2007
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes. it's funny how quickly one learns. since i first wrote that a couple of days ago, i've realized that the 'var' in my question is moot. but ignore the 'var' ignorance and the same question still applies. what is the overhead of repeated declarations of the same variable? and flanagan answers it.
    :-)

    thanks for taking the time to correct the error.


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
  •