JavaScript for…in vs for

前端 未结 22 1475
悲哀的现实
悲哀的现实 2020-11-22 07:15

Do you think there is a big difference in for...in and for loops? What kind of \"for\" do you prefer to use and why?

Let\'s say we have an array of associative array

22条回答
  •  无人共我
    2020-11-22 07:37

    there are performance differences depending on what kind of loop you use and on what browser.

    For instance:

    for (var i = myArray.length-1; i >= 0; i--)
    

    is almost twice as fast on some browsers than:

    for (var i = 0; i < myArray.length; i++)
    

    However unless your arrays are HUGE or you loop them constantly all are fast enough. I seriously doubt that array looping is a bottleneck in your project (or for any other project for that matter)

提交回复
热议问题