Recession-proof stocks can be a safe haven for investors during times of economic turmoil. When the economy slows, consumers look to stretch their dollars, often trading down from name brands and even choosing different retailers to buy from instead.

Budget-friendly retailers Walmart (WMT -0.15%) and Costco Wholesale (COST -1.64%) have business models that benefit from these recent shopping trends. Let's look at which consumer staples stock makes a better buy in today's market.

Walmart: For those seeking great value

Through pandemic lockdowns, recession fears, and persistent inflation, Walmart's defensive business model has remained stable, although the company faces its fair share of challenges. As the current economy reshapes consumer behavior, Walmart's discount prices have converted higher-income shoppers. The numbers don't lie, and these more affluent shoppers boosted second-quarter sales substantially.

Produce worker at Walmart strolls by amicably.

Image source: Walmart.

Seeking food and essentials at lower prices, higher-earning consumers (those with annual household incomes of $100,000 or more) accounted for approximately 75% of Walmart's second-quarter market share gains. In-store sales saw a 6.5% increase and online sales jumped 12% year over year.

Despite sales gains in food and essentials, Walmart saw sales of casual clothing, TVs, and other electronics dwindle in the quarter. Now sitting on a surplus of merchandise, the retailer is making efforts to reduce that inventory -- primarily with markdowns -- that have taken a toll on profit margins. Recognizing the shift in consumer spending this summer, it lowered its profit forecast for the full year.

Undeterred by challenges, management now looks to boost business with its subscription service, Walmart+, which offers such perks as unlimited free delivery from stores as well as fuel discounts. CEO Doug McMillon believes is an integral part of the company's long-term success.

Meanwhile, Walmart is making progress on reducing its surplus inventory, and aims to continue snatching market share in the grocery department.

Costco: Lasting success

With over 110 million Costco cardholders worldwide, the chain retains some of the most loyal bargain hunters out there. The warehouse retailer earned $4.2 billion in fiscal 2022 from membership fees alone, helping to keep prices down and lure more members. Also worth considering: that $4.2 billion accounted for over 70% of Costco's annual profits.

Recently closing out fiscal 2022, Costco continues to perform well in the current market. The wholesaler enjoyed a 16% jump in net sales for the year, totaling more than $222 billion, compared to last year's $192 billion. And last quarter's sales increased more than 15% year over year.

One key advantage Costco holds over Walmart is its low rate of theft. A tight control on shoplifting keeps Costco's shrinkage to 0.12% of sales. In comparison, Walmart loses an average of $3 billion a year from theft. Other ways Costco passes savings on to customers include strategic vendor agreements and a near-zero advertising budget. 

Considering its stock has more than quintupled over the last decade, Costco has certainly rewarded long-term shareholders. And although investing legend Warren Buffett sold his stake in the company a couple of years ago, billionaire Charlie Munger (Buffett's right-hand man) still holds his shares -- with no intentions of selling. Time will tell whether Buffett or Munger has the right instincts.

Which is the better buy?

To help gauge whether Walmart or Costco is the better buy, let's look at current market capitalizations, price-to-earnings ratios, and dividend yields.

Metric Walmart Costco
Market capitalization $378 billion $220 billion
Price-to-earnings ratio 27.8 37.8
Dividend yield 1.61% 0.72%

Data source: E*Trade.

A lower price-to-earnings ratio and a higher dividend yield make Walmart today's winner. But for a long-term investment, you really can't go wrong with either of these retail titans, which are both well-positioned for today's consumers.