Could be a separate change, but if axis only has two opposing elements like it does here, this could be axis.reverse(); which should be faster than your reverseAxis() function using regex.