Caching arr.length and simplified the if conditional per ericf's suggestions.