fixed the performance issue by replacing all Array.shift with Array.slice