$.each is very slow. Should try to convert to native for loop or reverse while.