1

I have a for loop which iterates through all items:

for (i=0; i < this.widgets.length; i++) {
  this.changeWidgetArray(this.widgets[i]); 
}

In the for loop, for every single element, I call "changeWidgetArray". In this method, the "widgets"-Array from above is changed. Does this array-changing in "changeWidgetArray" affect the loop iteration?

My question is:

In the moment, when the iteration begins with the first item, is the whole array with all of its elements "collected", or does JavaScript dynamically fetch one by one before the next iteration step begins.

Or does this depend on the JavaScript implementation? Is this defined in the ECMA-Script specification?

Thanks alot in advance

2
  • It depends on where this array come from Commented Jan 23, 2013 at 8:05
  • @SergeS: I do not understand you. Commented Jan 23, 2013 at 8:08

3 Answers 3

5

YES:

for (i=0; i < this.widgets.length; i++) {
  this.changeWidgetArray(); 
}

NO:

for (var i=0, max=this.widgets.length; i < max; i++) {
  this.changeWidgetArray(); 
}
Sign up to request clarification or add additional context in comments.

4 Comments

You might want to mention accessing invalid indexes if the length is cached while the array is changed .
Why the downvote? It's a simple yes/no question and this answer is correct. No need to get into gotchas or best practices refactoring here.
^ No worries I know, it came like 6 minutes after your comment. Anonymous downvotes are the silliest thing about this site. They should require a reason at least.
I completely agree. It can be very frustrating to be downvoted for no apparent reason.
2

During the loop iteration, no variables are cached. (Unless you do that yourself in temporary variables)

So, this.widgets.length will be the exact length of the widgets array, each time it's evaluated, because the value of length is updated each time the array is changed.

You can cache the length by assigning it to a temporary variable like this:

for (var i=0, l = this.widgets.length; i < l; i++) {

But that might be a bad idea, since you're changing the array in the loop, possibly making you point to invalid indexes in the array.

Comments

0

The array length is updated every time you modify the collection it contains. Any time you reference array.length, it will reflect the current size of the array.

For that reason, as well as many performance benefits, you should always write your code like this:

for(var i = 0, l = array.length; i < l; i++){
  // Do stuff with array[l]
}

You can also eek a little more performance if you do it like this (assuming the direction you traverse doesn't matter):

var l = array.length;
while(l--){
  // Do stuff with array[l]
}

3 Comments

"you should always..." Nope. That's a very bad idea if you're messing with the array you're iterating over. Don't tell someone to "always" do X.
@Cerburs. So you say it is not good to mess with an array while iteration over it?
@WolfgangAdamec: No, what I'm saying is that: 1:If you do change a array's amount of content while iterating over it, it's mostly a bad idea to cache the length. And 2: if you don't change this, you can cache the length.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.