When a spring is compressed to extreme distance −xm, the kinetic energy K decreases due to resistive internal forces, while the potential energy U increases.
The same is true for expansion of spring.
This variation is shown below, where parabolic plots of the potential energy U and kinetic energy K of a block attached to a spring obey Hooke's law. The two plots are complementary,
i.e. one decreases as the other increases. The total mechanical energy, E=K+U remains constant.