Performance shouldn't be your first priority. Of higher importance should generally be code readability and maintainability. Exit For
states, in code, exactly what you want to do, semantically. It's blatantly obvious to the reader exactly what the code is doing.
When setting the loop variable it's a lot less clear. I need to spend time trying to figure out what's going on; is the loop variable set to the end, ahead one, the start, somewhere in the middle, or what? That's time I shouldn't need to be spending.
Beyond that, you're also now repeating the constant 10
in that loop. What if someone goes to change the loop variable to go to 15 instead. They may not bother to look through the entire body of the loop and just expect it to work fine going up to 15 instead. You now have a very evil bug. When I come along to debug the code I may not know if someone intended the loop variable to be moved to another internal location, or if it really should be the end of the loop.
Another key difference is that End For
ends the current iteration of the loop right now. Setting the loop variable means that the current iteration ends and then the loop stops. If there is more code in the current iteration of the loop then that's different. It's only the same if the code is at the end of the loop.
As for the performance differences, I wouldn't expect there to be any particularly significant difference. In the event there is a difference at all, it's almost certainly going to be very, very, very tiny, and not enough to matter. If, by some surprising series of events you've managed to determine that this path of this loop is executed so frequently, and in a context where performance is so vital, that this change results in a noticeable and essential performance difference through your in depth performance tests and profiling, then consider using the less readable version (possibly with supporting comments).
Best Answer
It is not necessarily wrong if the code does what it is supposed to do, however, decisions such as this can be a matter of taste or opinion in personally written code. Conversely, avoiding this particular arrangement of logic may be a requirement of an employer.
Here are the things to consider.
Edit
To be more specific, here are some pros and cons using the code in your example.
Pros
Cons
undefined
,null
,0
,false
,NaN
,""
or has "holes" in it, this way will fail."Holes" means when the array has a length greater than 0 and some (or all) of the elements of the array have not been assigned a value.
"Truthy" means values that do not evaluate to false ( see the cons ). Do not forget
NaN
or""
, I almost just did.The code in your example already contains a bug. The first element in the array will be missed. Why? The first element is zero, which when used in an assignment statement, returns the value zero, which is treated as false by the while loop, meaning the while loop stops before processing the last item on the "stack", or rather the first element in the array.