Cache for images length onload

Cache the images.length value in a variable instead of recalculating on
each loop. Doesn’t make much difference in newer browsers but older
ones can see a large difference. See
https://stackoverflow.com/questions/6973942/is-optimizing-javascript-for-loops-really-necessary