Optimises loop

Gets categories.length once instead of on every iteration of loop.