0

According to this JSPerf the While ! Undefined style array loop is ~10x faster in all browsers. What are the disadvantages of this style of loop?

4
  • 1
    It'd be a problem with sparse arrays. Commented Jun 18, 2012 at 20:33
  • @Pointy, What do you mean by 'spare arrays'? An array like this: [0,1,,3,undefined,,,]? Commented Jun 18, 2012 at 20:36
  • Yes - arrays that have unset entries in them. May or may not be a problem for you. Commented Jun 18, 2012 at 20:36
  • ... or entries that are undefined. Commented Jun 18, 2012 at 20:38

4 Answers 4

1

It is actually a bug in the test case, the iterator is not being reset to zero for each test run (i.e., only the first test run rolls the full loop, next runs have the iterator already past the end of the array, thus roll zero times). See corrected test suite for the true benchmark.

(Note: I haven't inspected all test cases, others might be flawed as well)

Sign up to request clarification or add additional context in comments.

3 Comments

Good observation. I would have upvoted if the answer contained an answer to the question.
I assumed it is obvious: the main disadvantage of that style is that its presumed advantage is nonexistent.
The "order" thing was flawed as well, it sliced but stopped at first iteration. correction :-)
1

As you can see, we're talking about 10x faster/slower on a scale of millions of operations per second, which is not significant enough to worry about.

A possible disadvantage of that style is readability to other developers, which is more important than the "performance boost".

Judge yourself, what's more readable?

// Chrome 21: 5M operations per second
var a;
while ((a = arr[i++]) !== undefined) {
    someFn(a);
}

// Chrome 21: 324k operations per second
for (var i = 0; i < arr.length; i++) {
    someFn(arr[i]);
}

2 Comments

I agree wholeheartedly about readability. From a purely performance standpoint, readability aside, are there any apparent disadvantages/pitfalls/gotchas?
@caseyohara Other than the inability to use undefined as a value, not. But as lanzz points out: The test case is flawed, and this is the correct benchmark. Now, the differences are even slower, which strengthens the readability point.
0

The major disadvantage I can see is that you can't break out of the loop! You'll hit an unresponsive UI in no time with that.

1 Comment

All of those code samples share that characteristic. I mean, they're all just iterations. That one will definitely terminate, as soon as it gets past the last item in the array.
0

disadvantages: 1. if a[i] has been used it is no longer undefined. Thus you may do more than anticipated. 2. readability, it is difficult to know the end point (unless you put some comments ;) nothing else.

the new revision still doesn't differ that much, but if it's speed you need then comment well and test well.

If your function "someFn(a);" has a timer of more than in these tests then i'd recommend testing your own loop if it is that important.

If not always stick to tidy coding.

Comments

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.