Flirting is a way to test whether the object of your attention is a) available and b) could be potentially attracted to you.
It is to some extent a game that involves less risk than making a serious and bold approach to someone in whom you are interested. If the other party responds favourably, then there is less risk in a more direct and sincere approach. If your attention is not returned in kind, the game-like nature of the process leaves your ego intact and the other party can be pleased that they are attractive, even if they are not, in this case, interested in the flirt.
If you are in a committed relationship, then you have no business flirting unless you would do so if your loved one were right there and would be completely comfortable with your behaviour. If your loved one does not mind, shouldn’t you be concerned that it does not bother them?