In my opinion the best way is to at least try to keep your calm in such situations. It's all about their point of view against yours - their subjective world and yours.
If you go and get angry about it, throw something back, it means that you have fallen to the level of their subjective world.
People respect those who keep their own side without being weak (at least in male world). You should also think hard about whether you yourself provoke such reactions in every way. People who just suddenly snap with negative comments without any reason just look like dicks and there is nothing you can do except keep yourself composed.
There is ultimately no one who can survive negativity without getting affected by them at all. Even the famous people get hurt even though people often think that they shouldn't care.
Ignoring works very well. People want reaction, the get energy from it
. People who throw negative comments have a negative energy inside them which they are projecting to the world around them.
Short pause, maybe look them shortly in to eye and continue doing whatever you were doing / walk away.
Even though it feels like "doing something" would have been the best choice, always know that not reacting is the best way almost every time.
There is an exception like if someone is continuously harassing you, then action is definitely needed. If you ignore once, twice or maybe three times, it's still good, but after that it will only look like you don't have it in you to take action.