www.webdeveloper.com
Results 1 to 14 of 14

Thread: JavaScript Loops Performance

  1. #1
    Join Date
    Nov 2009
    Posts
    2

    JavaScript Loops Performance

    Hi,

    Which among the following for iterating objects in JavaScript is better in terms of performance & usage?

    A) property enumeration:
    for (var propName in someObject) {
    var value = someObject[propName];
    }

    B) Array protocol
    for (var i=0; i<someObject.length; i++) {
    var value = someObject[i];
    }

    C) forEach method

    Can forEach method be used across various browsers and OS?

    K

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    First: the loops for(a in obj) and for(var i=0;i<array.length;i++) should be used according to the nature of the collection. If the collection is an object, use first loop (because an object has no length property, or better say it has the length=0), if the collection is an array, use the second one. Thus, the speed comparison has no base.

    Second: javascript 1.2 has no foreach loop. In fact it has, but the equivalent of a foreach loop is exactly the first loop you have mentioned, the loop for unordered collections (objects): for(a in obj)

    Third:
    - a backward loop is faster than a normal one:
    Code:
    for(var i=array.length-1;i>=0;--i)
    - a while loop is faster than the for() loops
    Code:
    var i=0, a;
    while(a=array[i++]){
    ...//do something with a
    }
    - a switch/case statement is faster that any loop. But for a "long" array (or an object with many properties) it is not practical.
    Last edited by Kor; 11-12-2009 at 09:31 AM.

  3. #3
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    forEach is supported in all latest browsers

    Loop optimization tests: https://developer.mozilla.org/en/Cor.../Array/forEach
    Always test using your own code and data!
    At least 98% of internet users' DNA is identical to that of chimpanzees

  4. #4
    Join Date
    Nov 2009
    Posts
    2
    Thanks for the quick comments - it does make some things clear!

    Regarding your third point, why is backward loop faster than normal one?

    forEach details can be found in Mozilla website under this link https://developer.mozilla.org/en/Cor.../Array/forEach

    I am not sure if it has been made a standard yet.

    K

  5. #5
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by Fang View Post
    forEach is supported in all latest browsers
    Not in IE7

  6. #6
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by karthik085 View Post

    I am not sure if it has been made a standard yet.

    K
    It is not a matter of standard, it is a matter of the javascript version. IE7 does not use the JavaScript 1.5 core (and forEach() method is a specification of JavaScript 1.5). I don't know about IE8.

  7. #7
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Quote Originally Posted by Kor View Post
    Not in IE7
    IE7 is not the latest browser
    At least 98% of internet users' DNA is identical to that of chimpanzees

  8. #8
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by Fang View Post
    IE7 is not the latest browser
    Well, it is one of the latest browsers. The problem is that IE7 is still in use, and something tells me it will be at least 1-2 years...

  9. #9
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    You might want to add a zero to the year estimate ...
    At least 98% of internet users' DNA is identical to that of chimpanzees

  10. #10
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by Fang View Post
    You might want to add a zero to the year estimate ...
    In fact I am one who has not switched to IE8. But I have a good reason: I am a web designer and programmer and I need to know how a page looks like in IE7 rather that IE8 - assuming that what works in IE7 will work in IE8 as well, mainly if I use a silly but useful meta tag as :
    Code:
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
    I could use a virtual machine to run IE7 and IE8 in parallel, but it does not worth the effort, so far

  11. #11
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Use the developer toolbar (F12 key) in IE8 to switch between IE8, IE7 and quirks mode. Works quite well.
    At least 98% of internet users' DNA is identical to that of chimpanzees

  12. #12
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by Fang View Post
    Use the developer toolbar (F12 key) in IE8 to switch between IE8, IE7 and quirks mode. Works quite well.
    Does it? Hmm... all the "parallel IE" applications I have used so far were not quite accurate, mainly for IE6 (oh, how I hate that version...). I suppose it is the time to give that IE8 facility a try. Moreover, IE8 looks like a decent browser. Thanks for the tip.

  13. #13
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    'developer toolbar' is an integral part of the browser, not 3rd party.
    At least 98% of internet users' DNA is identical to that of chimpanzees

  14. #14
    Join Date
    Sep 2009
    Posts
    52
    IE7, IE8? Luck you. Most of the corporates I produce systems for still use IE6. It really is fun how they expect widget heavy desktop like AJAX apps to run well in IE6. We show them how it runs in Chrome and they say "wow" but will they upgrade? No!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles