Minor optimization in loop - cache length