Costco Wholesale (COST -0.82%) has been a huge winner for investors. Shares have soared 238% in the past five years, a much better gain than the S&P 500. And they currently sit at an all-time high.

It's been a different story for Home Depot (HD -0.77%). The home improvement chain's shares have underperformed the S&P 500 in the last five years, and they're 20% below their peak price.

Which of these two leading retail stocks is the better buy right now?

Enthusiastic investors

Because Costco shares are trading at record levels, mimicking the broader market indexes, it's accurate to say that investor enthusiasm and bullishness toward the business has never been higher. This is clear when considering the valuation.

As of this writing, the stock sells at a price-to-earnings (P/E) ratio of 50. This is about as expensive as the stock has sold for in the past 25 years. And shares trade at a 46% premium to their trailing-10-year average.

Some might argue that Costco deserves this sky-high valuation. The company has taken care of its shareholders with impressive returns and special one-time dividends. Its scale creates a powerful competitive advantage that allows Costco to sell high-quality goods at extremely low prices. A membership model leads to customer loyalty. And Costco has proven that it can steadily increase sales and earnings no matter what the economic conditions are.

I don't disagree with all of these positive attributes. But I'm in the camp that believes Costco's forward returns probably won't outpace the S&P 500. Executives will naturally run out of locations to open new warehouses, especially at the same pace as when the business was much smaller. Consequently, revenue growth should decelerate in the years ahead.

According to Wall Street consensus analyst estimates, Costco's revenue and diluted earnings per share are forecast to rise at compound annual rates of 6.3% and 11.2%, respectively, between fiscal 2023 and fiscal 2026. That doesn't come close to justifying the current valuation.

Macro headwinds

After posting double-digit revenue gains in fiscal 2020 and fiscal 2021, Home Depot's business has been dealing with a notable slowdown. Executives point to the unfavorable macro environment as the culprit, as inflationary pressures discourage consumers from taking on bigger renovation projects. This helps explain a 3.2% same-store sales drop in fiscal 2023. Management expects this figure to fall 1% in the current fiscal year.

However, I view these challenges as being temporary in nature. While Home Depot is the clear leader in the $1 trillion home improvement sector, it has just 15% market share. The fragmented nature of the industry means that a scaled operator, one with brand recognition, unrivaled inventory availability, and wide reach with its more than 2,000 nationwide stores, can steal share over time.

Investors will also appreciate Home Depot's unwavering commitment when it comes to returning capital to its shareholders. Last fiscal year, the company paid $8.4 billion in dividends. And it repurchased $8 billion worth of stock. The consistent generation of free cash flow means this capital allocation policy is safe.

It also helps that Home Depot's shares are reasonably priced. They trade at a P/E multiple of 22.5. That's about in line with its trailing-10-year average. And it represents a slight discount to the broader S&P 500.

It's clear that Home Depot is more sensitive to macro conditions than Costco is. And this hasn't been more in the spotlight than it is right now. But I believe the former's valuation and attractive industry backdrop create a more compelling investment opportunity today. Investors just need to maintain a long-term time horizon.