Earnings season is underway and it has only added to the broader stock market's volatility over the past several months. Major moves to the upside and downside seem like the norm nowadays for many stocks as investors struggle to process a mixed bag of company-specific pros and cons and macroeconomic issues.

So far, Walmart (WMT -0.32%) and Costco Wholesale (COST -0.11%) seem to be somewhat immune to the volatility. They aren't just beating the market in 2022; they've produced positive total returns and are both trading around all-time highs. Let's take a closer look and see why they seem to be holding up so well in a volatile trading environment and maybe even compare the two to see which consumer staples stock is the better buy now.

Two people washing dishes in the kitchen.

Image source: Getty Images.

This discount-store dynamo is looking like a good deal

Share prices of Walmart reached a fresh all-time high on April 21. The company is recession resilient because its goods are affordable, and if anything, folks tend to shop more at Walmart and less at higher-end retailers during an economic downturn like we are currently facing.

What's more, the business is also resistant to inflation. When prices are rising, consumers are incentivized to cut back on discretionary spending, whether that means eating at restaurants less often or buying home goods at lower prices. These trends benefit Walmart because it usually provides the cheapest options in several product categories.

The retailer has a gross profit margin of 24.4% and an operating margin of 3.85%. That thin operating margin of less than 4% shows just how little profit Walmart is making on its billions in sales. When a customer spends $100 there, the company is making an average of less than $4 in profit from the transaction. 

Walmart's strategy is high volume and low margin. This strategy is built for a recession because the company can afford to undercut the competition. It has successfully expanded its e-commerce business in recent years as well, with e-commerce making up 13% of its record 2021 sales of $568 billion. 

Costco could be overvalued

Costco benefits from the same types of consumers looking for deals in a recessionary economic environment. Its business model is a bit different and involves bulk purchases of a select group of popular products sold at low-margin pricing to members who pay an annual subscription for the privilege of access to its goods.

Costco stock has a history of crushing the market (at least in comparison to many of its retail counterparts and competitors), and its stock price is close to its all-time high. Costco stock has been outperforming the broader market as well as major brick-and-mortar or online retailers. The stock has produced a total return of 138.8% over the last three years, which is more than double that of other consumer staples giants like Procter & Gamble (PG 0.65%), Walmart, or even Amazon (AMZN -1.11%) Surprisingly, direct competitor BJ's Wholesale (BJ -1.28%) is putting up similar stock performance and grocery giant Kroger (KR -0.04%) is also keeping pace.

COST Total Return Level Chart

COST Total Return Level data by YCharts

The strong return has resulted in Costco stock's elevated valuation metrics, which suggest the stock may be overpriced by comparison. Costco's forward price-to-earnings ratio tops 40 and it's roughly twice that of competitors like Walmart and BJ's and more than triple that of Kroger's.

COST PE Ratio (Forward) Chart

COST PE Ratio (Forward) data by YCharts

A similar business for a much more expensive price

Costco appeals to a different clientele than Walmart and is more focused on bulk offerings and gourmet meats and seafood, and has higher-priced apparel and appliances, too. But it's very similar to Walmart in that it relies on high volumes and razor-thin margins.

Costco has a much lower gross margin than Walmart and even a lower operating margin at just 3.5%. But its revenue is barely over a third as high as Walmart's even though its market cap is 60% of Walmart's. That is another indicator that Walmart's stock may just be the better value.

WMT Revenue (Annual) Chart

WMT Revenue (Annual) data by YCharts

A brief look at dividends

Walmart has a dividend yield of 1.4% which looks much better than Costco's 0.65% yield. But Costco has a reputation for periodically paying sizeable special dividends. For example, the company's quarterly base dividend is $0.90 a share. For most of 2020, it was $0.70 a share. However, Costco paid a one-time $10 per share dividend in December 2020. It also paid a special dividend of $7 per share in 2017. In fact, if you add up all of Costco's dividends over the last five years, it equals $29.84 per share, which works out to a yield of 1.1%. It's not great, but the special dividends certainly show why Costco is a better dividend stock than it may appear at first glance.

It's also worth mentioning that Walmart is a Dividend Aristocrat with 47 consecutive years of annual dividend hikes, but lately, it's been raising its dividend only minimally. It has simply raised its quarterly payout by $0.01 per share for the last eight years like clockwork -- which isn't all that exciting for income investors. Given the choice between a low ordinary yield from Costco but its willingness to return value to shareholders through special dividends or Walmart's consistent dividend, many investors would probably prefer Costco's approach. 

Why Walmart may be the better buy here

There's no denying that Walmart and Costco are cash machines. They generate plenty of revenue and, despite low margins, manage to return that revenue to shareholders through earnings, stock performance and dividends. Both companies have carved out a crucial slice of the retail market for themselves and have the scale and sophisticated supply chains to out operate any competition that comes their way. 

Costco's lofty valuation right now is predicated on sustaining a high growth rate to drive its top and bottom line and hopefully lead to more dividend raises. But if its growth stalls even a little bit, it could pressure the investment thesis for the stock.

Some investors may prefer to scoop up shares of both of these safe stocks. But given Walmart is just as efficient as Costco from an operating margin perspective with a far lower forward P/E ratio, Walmart seems like the better value as a stock right now.