performance improvement: cache array length for use in for loop