For loop optimization

you want to cache `length`s because otherwise the second portion of the `for` loop will have to call the variable every time, even though the value doesn't change