Many companies have been struggling as inflation remains a huge problem, consumer confidence is at historically low levels, and recession fears are worrying everyone. Even blue-chip businesses like Costco Wholesale (COST 0.17%) and Home Depot (HD -1.77%), which many investors likely view as safer options no matter what the macroeconomic picture looks like, are facing slowdowns. 

It's not all bad news, however. I think both companies have positive traits worthy of a closer look. But which of these major retail stocks is the better buy? 

Costco's economic moat is unmatched 

Legendary investor Warren Buffett's core philosophy is that he always seeks businesses with some kind of economic moat (attributes that allow them to defend themselves against the competition). It's hard to argue that Costco doesn't have one of the strongest moats around. In fact, Buffett's right-hand man, Charlie Munger, is a huge fan of the company. 

Costco's massive scale, as measured by trailing-12-month sales of $231 billion, makes it the world's third-biggest retailer. This lets the business control negotiations with suppliers, obtaining favorable pricing on its merchandise assortment. And these cost savings, propelled by Costco's no-frills shopping environment, are passed on to customers in the form of low prices. These customers seem to love it, as evidenced by Costco's global membership renewal rate of 90.5% in the latest fiscal quarter.  

Shareholders can appreciate Costco's economic moat even more considering that it has held strong even as Amazon changed the way people shop. Over the past five fiscal years, Costco's revenue has increased at a compound annual rate of 12%. This is faster growth than what the company registered in the prior five years. Clearly, consumers still see tremendous value in shopping in-person at a Costco location. 

Costco's current valuation, at a price-to-earnings (P/E) ratio of 38, is definitely not cheap by any stretch of the imagination. That's more expensive than the stock's trailing-five-year average valuation. But it might be worth paying a premium for such a durable and stable business. 

Home Depot dominates its industry 

With trailing-12-month sales of $156 billion, Home Depot is the undisputed leader in the home improvement market. About half the company's revenue comes from DIY customers, with the other half coming from contractors, plumbers, and the like. Over the past few years, those professional customers have become a more important part of the business, a positive trend since they tend to spend much more. 

Lately, inflationary pressures have been a headwind. Home Depot's same-store sales decreased 4.5% in the latest fiscal quarter (Q1 2023, ended April 30), and management noted that consumer spending is shifting away from goods. 

But the industry is highly fragmented, with Home Depot's market share estimated at just 17% right now. This tells me that there's a huge opportunity for the business to continue expanding. Home Depot's more than 2,000 nationwide stores put it within 10 miles of 90% of the U.S. population, a remarkable statistic. Smaller, independent home-improvement retailers will find it hard to compete with that advantage as well as Costco's impressive omnichannel capabilities. 

Home Depot shares trade at a P/E multiple of 18, a meaningful discount to its smaller rival Lowe's. But I think this valuation gap is unwarranted. Home Depot's average gross margin and operating margin over the past five years are both better than what Lowe's posted. And Home Depot's return on invested capital is better, too. It's likely only a matter of time until Home Depot's P/E catches up with, or even exceeds, its main competitor's. 

What do investors value more? 

Both Costco and Home Depot have valid arguments for why they deserve a place in your portfolio. But investors have to decide for themselves what they value most. If you care about finding the highest-quality businesses and you don't care about the P/E, Costco is a no-brainer stock to buy. But if valuation is a prime consideration in your decision-making process, then Home Depot is the logical choice.