As well as feeling darn good, masturbation may also help protect against sexually transmitted infections by flushing out the genital tract, at least in male primates, new research suggests.

"We find that masturbation is an ancient trait within the primate order," University College London anthropologist Matilda Brindle and colleagues explain in their study, in which they amassed the largest ever database of masturbatory instance records across 105 primate species.

It's already known that masturbation is widespread across mammals and other animals like birds. While some primates are notorious for self-touching habits – bonobos use mutual masturbation as part of their social repertoire, and macaques have been caught red-handed with sex toys – other species appear less inclined.

Since examples of masturbation can be found across the primate evolutionary tree, stroking one's own erogenous zones is likely an ancient pastime that was passed on to all primate groups by an early common ancestor.

But once the tennis ball-sized tarsiers took a different evolutionary road from apes and monkeys, masturbation became more common in some species than others, Brindle and team explain.

That the researchers found a pattern, rather than it being random, supports the idea that masturbation is more than just a fun, accidental side effect of sex. Pleasure clearly has its perks: it provides individuals with a compelling reason to mate and continue the species.

But that masturbation persists in so many species suggests pleasuring oneself may also have a specific purpose in and of itself.

The scientists analyzed records of observations in scientific publications and responses from primatologists and zoo-keepers, involving both male and female animals and wild and captive primates. They revealed masturbation is more common in males within species whose females mate with multiple beaus.

"Masturbation was also lost frequently in single-male mating systems, but almost never in multi-male mating systems," the researchers explain.

This suggests masturbation may somehow be increasing chances of fertilization in competitive sexual scenarios. While how is still not clear, one theory propounds it may improve ejaculate quality by expelling 'stale' sperm first.

But the team also found "strong evidence for coevolution between masturbation and pathogen occurrence in males", pointing to a secondary theory for why masturbation is common – to flush microorganisms that cause disease out of the genital tract. Masturbation is also more common in males of larger primate species that can't reach down to orally groom their genitals.

"Masturbation was lost at a very high rate when pathogens were absent but almost never when they were present," Brindle and colleagues note in their paper.

They propose that examining the timing of masturbation could reveal which of these trends are driving the selection of masturbation within each species.

If masturbation most often occurs before sex it would suggest it is improving male fertility, whereas after sex may indicate it's a sexually transmitted infection (STI) preventative. A 2010 study conducted on African ground squirrels engaging in post-sexual masturbation came to a similar conclusion.

Neither of these trends, however, were observed in the female primate masturbation data. But that does not necessarily mean masturbation doesn't also serve some sort of fitness purpose for them too.

"It is important to note that there are far fewer reports for masturbation in female primates in our dataset," Brindle and team point out. "This is in part because female arousal and masturbation can be less conspicuous than that of males, but also reflects a broader paucity of information of female sexual behavior and morphology in the biological sciences."

Even if there ends up being no such advantage for those of us without penises, participating in a ménage à moi has plenty of studied health benefits in humans. Solo fun times can help with sleep, pain relief, self esteem, and provide other health benefits.

So regardless of your sex, science supports you doing you.

This research was published in the Proceedings of the Royal Society B.