Starbucks (SBUX 1.85%) and Costco Wholesale (COST -0.67%) both withstood the S&P 500's 14% decline over the past 12 months. Starbucks' stock rose 7%, while Costco experienced a mild decline of 4%. Let's see why these two S&P 500 companies outperformed so many of their peers -- and if one is a more promising investment right now.

Starbucks is resisting some tough macro headwinds

At first glance, Starbucks might seem like a risky investment. The coffee chain is heavily exposed to inflation, which drives up its costs and curbs the market's appetite for its pricey beverages. And the COVID-19 lockdowns in China affected its second-largest market after the United States.

Starbucks also faces unionization demands in the U.S., while founder Howard Schultz -- who returned as its interim CEO last year -- is in the process of handing the reins to its new CEO, Laxman Narasimhan.

A person drinks coffee outside.

Image source: Getty Images.

Yet, Starbucks continued to grow through all those challenges. Its revenue rose 11% to $32.3 billion in fiscal 2022 (which ended last October), driven by an 8% increase in its global comparable-store (comps) sales. Its robust comps growth in North America and other markets offset a 24% comps decline in China, which was mainly caused by the government's unpredictable COVID lockdowns. Inflationary headwinds reduced its adjusted earnings per share by 5%.

Despite that bottom-line pressure, Starbucks still increased its store count by 3% and 8%, respectively, in its North American and international markets for the full year. It expects to open about 10,000 new stores by the end of fiscal 2025 to reach 45,000 worldwide locations.

It's been offsetting some of the inflationary headwinds in the U.S. with price hikes, and it expects China's growth to accelerate again as it relaxes its COVID restrictions this year. The unionization pressures in the U.S. are also gradually easing with fewer stores filing petitions to organize.

For fiscal 2023, analysts expect Starbucks' revenue and earnings to grow 11% and 16%, respectively. Based on those estimates, which are in line with the company's own expectations, its stock trades at 31 times forward earnings.

Costco continues to lock in its members

Costco grew rapidly through the pandemic as shoppers stocked up on household products, and it maintained that momentum after the lockdowns ended. It also remained a top shopping destination as inflation made bulk purchases more appealing.

Costco can afford to sell its products at such low prices because it generates most of its profits from its high-margin membership fees. Therefore, it should remain an evergreen investment as long its locks in its members, consistently raises its fees every few years, and continues to open new warehouses to reach new shoppers.

Those green flags are still flying. In the first quarter of fiscal 2023 (which ended in November 2022), its global renewal rate grew 140 basis points year over year to 90.4% and rose 90 basis points to 92.5% in the U.S. and Canada. Its number of cardholders increased 7% year over year to 120.9 million, while its total number of warehouses climbed 2% to 847.

In fiscal 2022 (which ended last August), Costco's revenue rose 16%, its adjusted comps (which exclude foreign exchange and gasoline sales) grew 11%, and its earnings per share increased 17%. Inflation has been curbing its sales of big-ticket items like consumer electronics and appliances, but it's been largely offsetting that pressure with brisk sales of food and sundries. On a reported basis, higher gas prices also partly offset the impact of a strong dollar on its overseas sales.

Analysts expect Costco's revenue and earnings to grow 7% and 10%, respectively, in fiscal 2023. Based on those forecasts, its stock trades at 34 times forward earnings.

The valuations and verdict

Starbucks and Costco both look a bit pricey relative to their near-term growth potential. That's likely because investors flocked to both stocks as safe havens as interest rates rose. So I'm not in a hurry to buy either of these stocks right now -- since their valuations could deflate if interest rates stabilize and a new bull market starts.

But if I had to choose one over the other now, I'd stick with Costco because its business model is more balanced, it's more resistant to recessions, and its membership plans are incredibly sticky. Starbucks' long-term expansion plans are promising, but they could still be easily derailed by a deep global recession.