Splice is O(n) making this whole thing O(n^2). Can you refactor into O(n)?