it looks like this method of iteration is much much faster than a for loop:
var arr = window.arr.slice(0),
fruit = arr.pop();
while (fruit) {
fruit = list.pop();
}
as evidenced in this jsperf test: http://jsperf.com/loop-iteration-length-comparison-variations/7
I know I’m taking a memory hit by cloning the array but if i delete the clone right after i loop through it, what else should i be weary of?
The main drawback is here:
If
fruitevaluates to false (i.e. is null, undefined, 0, and so on) the loop will stop. That is nasty for sparse arrays, e.g.will only loop over the last member, then attempt to evaluate
arr[arr.length - 2], find it returns undefined (it doesn’t exist) so the loop ends. This is avoidable in aforloop as you can test if the property exists first (if necessary):So the pop method might be fast, but can only be reliable if you are certain that none of the values evaluate to false, or the loop is terminated by keeping a counter:
so you might as well do
and not bother with the copy.