We can safely assume that Superman's heat vision moves at the speed of light, meaning that shooting across the cosmos would involve some delay for the light to get there.
But otherwise, it's just a matter of how focused the beam is: perfectly focused and it will continue forever. But if it's even slightly unfocused, it will gradually get dimmer as the distance increases, the way the brightness of a flashlight diminishes over distance, because the light cone has gotten wider.
So how focused is Superman's heat vision? Not very, under most circumstances. Most often, it's depicted as a rapidly widening cone, starting at eye-width but growing to several feet across by the time it reaches the opponent, even at close range.
That being said, Superman seems to be able to deliberately control how focused the beam is at any given time. He's routinely shown making fine, precision cuts with his heat vision, such as during emergency surgeries, and perhaps most famously when he lobotomizes supervillains during one of his many "dark Superman" arcs.
In those moments, the scars are often even smaller than Superman's eyeballs, indicating that the heat vision beam had narrowed during its path before reaching their foreheads.
If he's capable of a widening cone and a narrowing cone, odds are he's capable of a focused beam. It might take a lot of concentration to focus it so perfectly that it could remain focused across light-years...
...but this is Superman we're talking about. He can do it.