Avoid dom operation inside loop