Costco (COST 1.59%) has a fantastic track record of growing investors' capital. In the past five years, the dominant warehouse club chain's share price has soared by 218%. That's partly thanks to its superb financial performance.

It's also a much larger gain than Home Depot (HD 0.56%) has booked over the same period, though the home improvement heavyweight is up by a respectable 75%. More recently, Home Depot's business has dealt with a notable sales slowdown.

For investors looking to gain exposure to the retail sector, which of these large-cap stocks is the better buy right now?

Everyone knows Costco is an elite company

Costco is widely regarded as one of the best companies in the world. The stock's impressive performance is clearly indicative of how high-quality the business is. One obvious reason is its strong financial performance. Its same-store sales have consistently increased each year, which is the dream of every retail enterprise.

Selling a wide selection of merchandise at low prices isn't that exciting. However, Costco's membership-based business model is. It provides a predictable and recurring high-margin revenue stream that totaled $1.1 billion in the company's fiscal 2024 third quarter (which ended May 12), up 7.6% year over year. What's more, its business model encourages repeat store visits.

When it comes to these memberships, the company has proven pricing power. Management just announced it was hiking its annual fees for the first time since June 2017. Despite the occasional fee increases, Costco's membership count has continued marching higher, demonstrating the appeal of its value proposition to consumers.

Disruption is happening all across the economy, and the brick-and-mortar retail space certainly hasn't been spared. Dominant e-commerce player Amazon, for instance, has had retailers shaking in their boots for a long time. This hasn't prevented Costco from continuing to grow its revenue and earnings while expanding its membership base.

The issue for investors now, however, is that everyone is fully aware of Costco's merits. They are well reflected in the current stock price, which is near its all-time high. Additionally, the valuation is excessive. Shares trade at a price-to-earnings (P/E) ratio of 54, a level that the retailer has rarely reached before. It's fair to argue that returns will be disappointing from this point on.

Home Depot's struggles should be temporary

Home Depot focuses solely on the home improvement sector, which it has long dominated. Its fiscal 2023 sales of $152.7 billion were significantly higher than those of its closest rival, Lowe's.

After enjoying strong revenue growth during the pandemic, Home Depot has been struggling recently to hold onto those gains. Same-store sales declined 3.2% in its fiscal 2023 (which ended Jan. 28), and management believes a 3% to 4% drop is in the cards this fiscal year. Consumers are pulling back on their discretionary spending in the face of macroeconomic uncertainty.

These challenges are worrying, but for long-term investors, they should be viewed as a temporary blip. Home Depot still has a sizable runway to grow its share in what is a massive market. It benefits from the country's aging housing stock and the shortage of homes for sale. Given its brand power, broad reach, and unmatched inventory availability, it's in an enviable position.

Another reason to be bullish on Home Depot is its favorable capital allocation program. Management allocates a large slice of its consistently generated free cash flow to dividends and share buybacks. In fiscal 2023, it distributed a whopping $16.3 billion to shareholders via those two methods.

Shares trade now at a P/E ratio of 24, which is less than half the valuation of Costco. To be clear, I believe the warehouse club operator is a better business, but I wouldn't touch its stock with a 10-foot pole right now because of its nosebleed valuation. For that reason, I believe that Home Depot is the better of these two retail juggernauts' stock to buy.