Okay...I just had to test my guess.
I ran Felgall's looping code inside of a 1-million times outer loop and got (average of 5 runs) 750 milliseconds.
I did the same with my code (using the subArray
but *NOT* using the ii and jj variables to hold the array lengths) and the average of 5 runs was 780 milliseconds.
Okay. So let's be complete about it. I then tried the original:
var str = "";
for (var i = 0; i < myArray.length; ++i)
for (var j = 0; j < myArray[i].length; ++j)
str += myArray[i][j];
HA!!! 735 milliseconds!!!
So, NEITHER Fellgall's nor my "improvements" are actually improvements when it comes to performance! In fact, they each case a very slight but measurable DECREASE in performance.
And, yes, my "improvement" is worse than Felgall's. So I eat crow on that.
But, given the *VERY* negligible performance differences, I'd still opt for my subArray
version just because of the clarity of coding. (I also strongly suspect that if we were doing something more complex with the sub-array element in that innermost loop my answer might show up better, but at this point I'm not going to bet on it.)
All the tests were done with Chrome browser. Clearly you can expect different answers with different browsers.
P.S.: Just for completeness, I tried moving the var
, and since I can now see it does not impact performance at all, I'm going to stick with putting them where I want them. Basically, the same way I would use them in other languages: Declare them where first used.