Regardless of whether the U.S. is officially in a recession or not, the previous earnings season's results included many signals the economy is slowing. That doesn't bode well for any company that derives its revenues from the advertising industry, as businesses typically slash their ad budgets when belt-tightening is called for. It's an easy expense to control, which is why ad spending fell 27% during the Great Recession in 2008.

However, two ad-related companies that can shrug off this trend are The Trade Desk (TTD 0.44%) and PubMatic (PUBM 2.05%). These two programmatic ad-tech businesses are somewhat insulated from declines in the broader advertising industry. As a bonus for investors, the companies are young and growing briskly. And even though their industry may face headwinds for some time, these two have the potential to outperform their peers and the market long term.

Opposite sides of a transaction

In a digital ad sale, there are two sides to the transaction -- demand (the ad space buyer) and supply (the ad space seller). The demand side consists of businesses that want to advertise their product or service to customers, whereas the supply side is any business that has ad space to sell, whether that's on a website, connected-TV platform, etc.

Ad buyers are looking for data about the users who visit a media platform or network so they can target their ads to specific audiences. Furthermore, advertisers don't want to plaster the same ads on the website for all visitors (like a billboard on a highway). They want ads targeted to each viewer.

That's where The Trade Desk and PubMatic come in.

PubMatic works with the supply-side media owners and publishers to ensure appropriate ads are presented to each audience. It also has many tools to maximize the revenue generated from these ad sales.

The Trade Desk is on the demand side, and its bidding platform ensures a company's ad campaign targets the right audiences and provides real-time feedback. In addition, among The Trade Desk's industry partners (225 of them and counting), clients can choose to advertise on many of the most popular platforms.

The reason why these ad-tech businesses will be steadier than the wider advertising industry is they help place the ads. The prices of ads may fluctuate based on supply and demand, but placement costs will stay relatively stable. These companies can lean on that relative advantage, even if volume across the industry declines.

So while many ad-focused companies had rough second quarters, these two excelled.

A strong quarter in a weak environment

The Trade Desk's second-quarter results were impressive with revenue growing 35% year over year to $377 million. It also posted an adjusted EBITDA margin of 37%. However, when stock-based compensation was factored in, The Trade Desk lost $19 million in the quarter -- primarily due to a $66 million performance grant to the CEO. Management also gave upbeat guidance for at least 28% revenue growth in the third quarter -- a number that's hard to complain about considering the slowing U.S. economy and other headwinds like inflation.

As for PubMatic, its revenue rose 27% year over year to $63 million. The smaller company also posted an identical 37% adjusted EBITDA margin in the second quarter, but PubMatic was profitable on a GAAP basis with net income of $7.8 million. PubMatic's guidance for 15% revenue growth might trail The Trade Desk's outlook, but it's still impressive in light of the few companies giving upbeat guidance right now, especially in the advertising industry.

Pay a premium for growth, or take advantage of a bargain

In terms of stock valuations, one of these two is much cheaper than the other.

The Trade Desk has long commanded a premium valuation, and it currently trades at 21.7 times sales. PubMatic is a much smaller company with a solution less popular and with less reach than The Trade Desk's. As a result, it trades at a much more affordable 3.9 times sales and 18 times earnings. That's right -- you can purchase PubMatic's earnings for less than The Trade Desk's sales.

Long term, I think The Trade Desk's market opportunity is more extensive than PubMatic's, but latter's stock is too cheap to ignore. Both make great buys now as they take part in the long-term secular growth of digital and programmatic advertising.