Costco (COST 0.34%) and Walmart (WMT 1.00%) are two of the retail sector's most resilient stocks. Both companies weathered fierce competition from Amazon (NASDAQ: AMZN) and expanded as other brick-and-mortar retailers crumbled. Over the past five years, Costco's stock soared 199% as Walmart's stock advanced 78%. The S&P 500 rose only 73% in comparison.

But should you buy either of these popular retail stocks right now? Let's take a fresh look at their businesses to find out.

A shopper takes products from a shelf.

Image source: Getty Images.

The differences between Costco and Walmart

Costco is a warehouse club retailer that mainly sells bulk and discount products to its paid members. It can afford to sell its products at such low margins because it generates most of its profit through its higher-margin membership fees. To keep growing, Costco needs to keep gaining new members, maintain high renewal rates, occasionally raise its fees, and continuously open new warehouses. More than 80% of Costco's warehouses are located in the U.S. and Canada.

Walmart is more diversified. It operates its namesake superstores in the U.S., Mexico, China, and other overseas markets; competes against Costco in the warehouse club market with its Sam's Club stores; and operates smaller brick-and-mortar banners and e-commerce websites across 19 countries. However, the company still generated more than 80% of its total revenue from its Walmart and Sam's Club's stores across the U.S. in fiscal 2024, which ended this January.

Costco continues to flourish in a challenging market

Costco is still gaining and locking in members. In the second quarter of fiscal 2024, which ended this February, its total number of cardholders grew 7.3% year over year to 132 million. Its worldwide renewal rate stayed flat year over year at 90.5%, but its renewal rate in the U.S. and Canada grew 30 basis points to 92.9%.

It's also opening new stores. It ended the second quarter with 875 warehouses, which marked an increase of 27 warehouses from a year earlier. It plans to open 30 new locations, including two relocations, throughout all of fiscal 2024.

In fiscal 2023, Costco's gross margin was compressed by its higher mix of lower-margin food sales and the slowing growth of its e-commerce business. But in the first half of fiscal 2024, its gross margin expanded again as its e-commerce growth accelerated. It's also widely expected to raise its membership fees in the near future.

Analysts expect Costco's revenue and earnings to grow 5% and 14%, respectively, in fiscal 2024, which ends this September. For fiscal 2025, they expect its revenue and earnings to increase 7% and 8%, respectively. Costco pays a paltry forward dividend yield of 0.6%, and its stock certainly isn't cheap at 46 times forward earnings, but its evergreen business model might justify its higher valuation.

Walmart has a bright outlook for the future

Walmart ended fiscal 2024 with about 10,500 brick-and-mortar stores, roughly unchanged from the previous year. However, its total number of customers served across those stores rose 6% year over year to 255 million. Its revenue and adjusted EPS both increased by about 6% in fiscal 2024.

Just over a year ago, Walmart set a long-term goal of generating 4% sales growth and more than 4% operating income growth over the following three to five years. It planned to achieve that stable growth by expanding its Walmart+ subscriptions, automating more than half of the processing volumes at its fulfillment centers by the end of fiscal 2026, building or converting 150 new locations by fiscal 2029, and remodeling hundreds of its existing stores.

Walmart kept pace with Amazon by matching its prices, expanding its own e-commerce platform, using its own brick-and-mortar stores to fulfill online orders, and rolling out more delivery and in-store pickup options. It's also trying to increase the stickiness of its Walmart+ subscriptions with more discounts, free delivery options, and streaming videos from Paramount. It's even in the process of buying smart-TV maker Vizio to counter Amazon's Fire TV devices.

Analysts expect Walmart's revenue and adjusted EPS to grow 4% and 6%, respectively, in fiscal 2025. But its stock isn't a screaming bargain at 26 times forward earnings, and its forward yield of 1.4% won't attract any serious income investors.

The better buy: Costco

Costco and Walmart are both reliable long-term investments. However, I believe Costco will continue to outperform Walmart for three simple reasons: Its business model is simpler, it's growing faster, and it leads the warehouse-club market. Its stock is pricier, but it should continue to attract more growth-oriented investors than Walmart.