Optimizes for loops

checking the .length property in the second argument for a for loop is
costly and can add up.  Thus I changed the corresponding for loops to
check .length in the first argument and assign them to var l.  The loops
then proceed to check against l each run through