Well, there is a lot happening in the two pieces of code,
so it is hard to say for sure what is making the
second version faster. But one thing you have improved
is replacing all that array copying with splicing. Even better
would be to manipulate just indices of the single @lines
array for your computations.
-Mark