Costco Wholesale (COST 0.02%) and Walmart (WMT 0.29%) are the two largest discount retailers in the industry. High inflation this year has led a growing number of consumers to look for savings. In turn, both companies have reported better-than-expected earnings at a time when many retailers are struggling.

This has helped Costco and Walmart provide market-beating returns this past year. Costco is down 5% and Walmart is up by 6%. Meanwhile, the S&P 500 is down roughly 15% year to date. But which of the two is the better buy today? Let's take a closer look to find out. 

The case for Walmart

Walmart's third-quarter earnings exceeded analysts' expectations with its revenue growing over 9% year over year. International sales and sales at Sam's Club, one of its ancillary companies, rose 10% and 7% respectively.

Walmart is well known for its everyday low pricing, which attracts and retains shoppers in good and bad times. However, as inflation puts more pressure on consumers, more customers are choosing to shop at Walmart for its savings. Particularly among middle-income earners. Its share of customers earning over $100,000 per year has increased notably in its grocery division.

Grocery continues to be a major growth driver for the company, something that should hold steady even if the economy were to worsen.

The case for Costco

Costco may have a similar product lineup to Walmart, but product sales aren't the stock's only source of income. The company also earns revenue from its annual membership fees. As a warehouse, Costco scores discounted pricing for buying in bulk, passing the savings on to its customers. In turn, it charges an annual membership fee to shop at its store.

In the third quarter, around 2% of its revenue came from membership fees. Its number of members increased by 10% this past year, revenue rose over 16% year over year, and renewal rates for existing memberships also remain healthy at over 92%.

In its last update, in October, the company reported 7% growth in revenue from the month prior, an indication that demand for its products isn't slowing. The holidays often lead to more gatherings and gift-giving, which could help boost sales for the company at the end of this year.

Which stock is the better buy?

If you look at profit margins and diluted earnings per share (EPS), two important metrics to illustrate retail stocks' profitability, Costco is clearly in a better position. Costco's EPS rose 12% while Walmart's fell by nearly 33%. Costco has also been able to hold a better profit margin than Walmart, which has seen its margin slip 37% this past year.

COST EPS Diluted (TTM) Chart

COST EPS Diluted (TTM) data by YCharts

Walmart is dealing with an inventory problem traced back to the pandemic shopping frenzy. It's tackling the issue by reducing its inventory quarter over quarter. But high inventory levels may continue to eat into its profit margin and earnings in the near future. On the other hand, Costco's warehousing structure means it doesn't face the same inventory challenges.

Walmart is up today, but Costco has outperformed Walmart over the past five-, 10-, 15-, and 25-year periods, providing a total annualized return of 15%, while the former provided a return of just 10%. However, Costco comes at a steep price of roughly $530 per share. It's also trading at a slight premium compared to Walmart. Costco's forward price-to-earnings ratio is 36, while Walmart's is just over 25.

Both stocks are well positioned to withstand inflationary pressures in the coming years, making them worthy buys for long-term investors. If growth is your goal and you're willing to pay a slight premium for more opportunities, Costco is the better buy. If you're looking for a more approachable cost per share or looking to score a better deal, then Walmart is the buy for you.