IN “LITTLE BRITAIN”, a television comedy, Daffyd Thomas, who insists he is “the only gay in the village”, tries to expose the homophobia of his fellow Welsh villagers by wearing outrageous clothes (bright red rubber shorts are a favourite) and picketing the local library. But he is constantly frustrated: the inhabitants of Llanddewi Brefi are all either tolerant or gay themselves.

The corporate world is not yet as gay-friendly as Llanddewi Brefi. But attitudes have changed dramatically. Some 86% of Fortune 500 firms now ban discrimination on the basis of sexual orientation, up from 61% in 2002. Around 50% also ban discrimination against transsexuals, compared with 3% in 2002. The Human Rights Campaign (HRC), an American pressure group, measures corporate policies towards sexual minorities in its annual “equality index”. Of the 636 companies that responded to its survey this year, 64% offer the same medical benefits for same-sex partners as for heterosexual spouses. Some 30% scored a fabulous 100% on the group's index.

Progress has taken place in a wide range of industries. The 100% club predictably contains plenty of talent-driven outfits such as banks and consultancies (including Mitt Romney's old employer, Bain & Company). But it also includes industrial giants such as Alcoa, Dow Chemical, Ford, Owens Corning and Raytheon. Lord Browne, the boss of BP who resigned after his sex life made headlines in 2007, said he always remained in the closet because “it was obvious to me that it was simply unacceptable to be gay in business, and most definitely the oil business.” Today Chevron, one of BP's toughest competitors, has a 100% rating.

Companies are competing with each other to produce the most imaginative gay-friendly policies. American Express has an internal “pride network” with more than 1,000 members. Cisco gives gay workers a bonus to make up for an anomaly in the American tax code. (If you are married, the cost of various insurance premiums is deducted from your pre-tax income, but if you are merely a partner it is deducted from your post-tax income.) Some companies vocally support gay marriage. In the past fortnight Lloyd Blankfein, the boss of Goldman Sachs, has accepted an invitation from HRC to become its first corporate spokesman for gay nuptials, and seven big companies, including Microsoft and Nike, have written to Congress to support the idea.

What caused this corporate revolution? Pressure groups such as HRC and Britain's Stonewall can take some of the credit. But mostly it happened because changing attitudes in society at large have reduced the cost of being gay-friendly, and raised the rewards. A generation ago in the West, creating a gay-friendly workplace might have upset heterosexual staff. Now it probably won't. But failing to treat gays equally is very likely to drive them to seek employment elsewhere. Since they are perhaps 5-10% of the global talent pool, bigotry makes a firm less competitive.

Being fair to gays is arguably simpler than being fair to women. Women really do differ from men in the amount of time, on average, that they take off to raise children. And there is no obvious answer to questions such as: “how much paid maternity leave should a small firm offer?” From an employer's perspective, gays do not differ from straights in any way that matters.

Sylvia Ann Hewlett and Karen Sumberg of the Center for Work-Life Policy, a think-tank, have tried to quantify the benefits of inclusiveness to companies. They discovered that 47% of gays who have come out of the closet say that they are “very trusting” of their employers, compared with 21% who are still in the closet. Some 52% of closeted gays said that they felt stalled in their careers, compared with 36% of non-closeted gays.

This makes sense. It is hard to give your best if you have to conceal an important part of who you are. Straight workers routinely plaster their offices with pictures of their families, which not only creates a pleasant working environment but also broadcasts the message: “I have kids. Please don't sack me.” Closeted gays find it harder to socialise with colleagues and build informal networks. They waste energy inventing excuses. “You have to watch everything you say and how you say it,” says one closeted executive. “You have to be excellent at the pronoun game.”

Being gay-friendly can attract gay customers, too. Witeck-Combs Communications, a consultancy, estimates that gay Americans spend $835 billion a year. In 2001 Merrill Lynch created a private-banking team that focused exclusively on the gay market, courting gay non-profits and providing seminars on financial planning for domestic partners. Within five years the group had brought in more than $1 billion of business.

Out of the closet and into a cubicle

The revolution is far from over. Nearly half of the respondents to the Center for Work-Life Policy's survey are still in the closet. And even the most enlightened companies cannot make up for intolerance in the rest of the world. It is hard to reach the top of a big company without serving a stint abroad. But homosexuality is still illegal in 76 countries—including such vibrant business hubs as Dubai and Singapore—and is punishable by death in Saudi Arabia, Iran and parts of Nigeria.

Still, the gay revolution in the workplace is remarkable. In most places, companies are more liberal than governments. In America, for example, until last year soldiers could be kicked out of the army for being gay, and 29 states still allow discrimination on the basis of sexual preference. In the coming years, the revolution is likely to gather pace. Younger workers are far more relaxed about homosexuality than their parents were. Indeed, many young heterosexuals would feel uncomfortable working for a firm that failed to treat gays decently. Companies vying to recruit them will bear this in mind.

Economist.com/blogs/schumpeter