Walmart (WMT 0.46%) has long outperformed during recessions.

Founder Sam Walton built the company on the promise of everyday low prices, and consumers have flocked to its stores for its value prices on everything from groceries to apparel to electronics.

But beyond its reputation for low prices, there's another reason why Walmart is wowing Wall Street at a time when rivals like Amazon and Target are flopping.

The retailer dominates the grocery business. It's by far the No. 1 seller of groceries in the U.S., especially when Sam's Club is factored in, and groceries make up a majority of sales at Walmart's U.S. stores, bringing in more than $200 billion in annual revenue.

Groceries offer a number of advantages for Walmart. They drive frequent visits since most customers need to restock their pantry every week or so. And while they're food shopping at Walmart, they might pick up something else they need, like socks, batteries, or headphones -- items that they might not have otherwise bought at Walmart. That hook explains why competitors like Target and Amazon have invested heavily in growing their grocery businesses, but they're still far behind Walmart.

For the retail giant, groceries also offer a key advantage during a recession, and that's that consumers need them regardless of the state of the economy. That's especially an advantage for Walmart as it can leverage its everyday low prices model to bring in new customers looking to save money and sell them whatever else they need.

A tale of two retailers

Thanks to its strength in grocery, Walmart posted comparable sales growth in the third quarter of 8.2% at its U.S. stores, and growth was even stronger in grocery. The company said comparable sales (comps) in grocery were up in the midteens year over year and, when accounting for the growth rates over the past two years (a two-year stack), they increased in the mid-20% range.

Walmart also said it had strong market-share gains in grocery with an increase in food units sold, reversing a decline in the second quarter. Management noted on the earnings call that three-quarters of the share gain came from households making more than $100,000, showing it is making inroads with a customer base that has typically preferred higher-end retailers.

That outperformance helped Walmart beat estimates on both the top and bottom lines, and it raised its guidance for the full year, lifting the stock 7% on Tuesday.

By contrast, both Amazon and Target fell sharply following their own earnings reports as sales in discretionary categories were sluggish. Amazon, which gets nearly all of its retail sales from discretionary purchases, called for revenue growth of just 2% to 8% in the fourth quarter. Furthermore, CFO Brian Olsavsky said on the earnings call that Amazon's customers are reassessing their purchasing power in the face of high inflation. Target, similarly, said performance in consumer staples categories like health, food and beverage, and household essentials was solid, but areas like apparel, home, and hardlines like toys and electronics underperformed.

A clear advantage

Like its peers, Walmart was cautious with its guidance heading into the fourth quarter. The company called for an adjusted earnings-per-share decline of 3% to 5% in the fourth quarter due to the risk of persistent inflation, especially in its general merchandise category, which is made up of discretionary items.

However, Walmart still expects to pick up market share in the current quarter. CFO John David Rainey said on the earnings call, "In this period of macroeconomic uncertainty, we believe that we are well equipped to continue gaining market share in an environment where consumers need to stretch their dollars further."

Walmart stock might seem expensive at a price-to-earnings ratio of 25, but safe stocks trade at a premium during volatile times, and Walmart has proven itself in the past. Not only is the retailer well equipped to ride out the recession and deliver a reliable income stream, but the company is also set up to gain market share thanks to its strength in grocery.

With a recession possibly around the corner, that's something that's worth paying up for.