I think that the main gain in the first loop is that a cached in len , and then used as one logical value: it takes a little time to simplify the loop condition. But! It will not create the same array as the output.
Suppose you have ten elements in: [0,1,2,3,4,5,6,7,8,9] - the loop will process [0] first, then [- len], which in our example is [nine]. The end result will be newArray = [0, 9, 8, 7, 6, 5, 4, 3, 2, 1].
So it doesnโt matter why it is faster, because it does the wrong thing.
source share