Inflation is tripping up even top companies that usually demonstrate growth and profitability, and the stock market looks like it's going to end 2022 on a down note. The S&P 500 is already off 17% this year although that's up from its 2022 lows. The S&P 500 has lost value in only 10 out of the last 50 years, so it's a fairly unusual event.

The S&P 500 is an index, and its performance reflects the average of the 500 stocks it tracks. Both Walmart (WMT 0.26%) and Costco Wholesale (COST 0.75%), which are S&P 500 components, are outperforming the overall average. Walmart stock is down less than 2% in 2022, and Costco is off by less than 10%. Which one of these is the better buy today? Two Motley Fool contributors frame the debate.

The inflation-beating favorite

Jennifer Saibil (Costco): Costco has long been a recession-resistant stock. Its operating model, which involves an annual membership fee, allows it to charge rock-bottom prices that attract customers when they are pinching pennies. That's why Costco is still posting double-digit sales growth two years after the pandemic started, even through inflation, and even as similar retailers are slowing down.

Costco's markups give it a gross margin of around 11% to 12%, and even less than that in recent quarters as it keeps its prices down despite rising costs. It was just 10.2% in the fourth quarter (ended Aug. 28). Other discount retailers, including Walmart, might have double that margin, and premium supermarkets are even higher. That leads to incredible consumer engagement for Costco members in general and even more so when prices are rising. It also means record membership renewals, which came in at 90.4% in the fourth quarter. 

Management has made significant changes in light of its own rising costs and supply chain problems. It is leaning heavily into its own logistics operations, and taking control of its supply chain is a win-win for the company and its customers. Previously, Costco worked primarily with a drop-ship model for e-commerce, particularly for deliveries of big and bulky items. The benefits of that model are that Costco didn't need to buy inventory until it made a sale, and it didn't need to invest in shipping infrastructure.

However, its decision to change course has more than doubled its big and bulky deliveries at a lower cost of merchandise, and average delivery time has decreased from 15 days to less than five.

Costco's commitment to low prices affects everything it does, and it benefits shareholders in addition to customers. 

Ready for a recession

Jeremy Bowman (Walmart): If there was ever a good time to buy Walmart stock, it would seem like now. Rival Amazon is flailing, looking for areas to cut costs as it forecasts revenue growth of just 2% to 8% in the current quarter. And most economists think we're headed for a recession as well.

Walmart tends to shine in such times, since consumers know they can count on it for everyday low prices, and grab anything they need at its stores.

Walmart's strength in groceries, which make up a majority of its sales, also gives it an edge in a difficult economy because consumers need to buy groceries regardless of the health of the economy. 

Costco is also known for bargain prices, but there's a key difference between the two companies. Costco's buy-in-bulk model means it's difficult to leave the store without spending a lot of money, and it traditionally caters to a higher income bracket than Walmart, which focuses on low- to middle-income shoppers.

Consumers tend to trade down in a recession, and when they do, they're much more likely to trade down to Walmart than they are to Costco, whose membership fee also creates a barrier to customers who are pinching pennies.

Lastly, the best reason to choose Walmart stock over Costco might be the valuation. The former trades at a price-to-earnings ratio of 23.5, compared to the latter's 39.

Not only is Costco's stock nearly twice as expensive as Walmart's, but Costco's valuation also makes it vulnerable to multiple compression during a recession because the stock is trading at a premium to its historical levels.

Walmart, on the other hand, has a track record of outperforming during bear markets, including the current one (the stock is essentially flat year to date). As long as the company can control its inventory over the coming quarters, Walmart looks like a smart bet to beat Costco -- and the rest of the market -- during any recession, and come out stronger on the other side.

Which stock is the better buy?

Costco and Walmart are both discount retailers and tend to perform well when the economy is pressured. Historically, Costco stock has strongly outperformed Walmart, and there isn't any reason that shouldn't continue. But Costco stock is expensive right now, and it might slow down before picking up again. The market is currently favoring cheaper-value stocks, and Walmart offers that for investors.