optimized foreach