Some experts are saying a recession could happen this year. If it does, investors will be looking for suggestions on safe stocks to hold. Such an economic event is likely to have widespread impacts in every sector, even if only indirectly. Luckily, there are some stocks that are more resilient to the negative effects of a downturn.

Three stocks that outperformed the S&P 500 during the 2007-09 Great Recession were Gilead Sciences (GILD 0.07%)McDonald's (MCD -0.05%), and Walmart (WMT 1.32%). Let's take a look at why these three stocks are recession resistant (including resistance to the effects of inflation), and why they make safe investments to hold in 2023.

^SPX Chart

^SPX data by YCharts

1. Gilead Sciences

Gilead Sciences is a top healthcare stock that is safer than most in a recession. And a big reason for that is because the nature of the treatments it offers, which are vital to its patients.

HIV drug treatments are a key part of its operations, with products in that segment accounting for roughly 75% of its core business (which excludes COVID-19-related revenue). For the nine-month period ending Sept. 30, HIV-related product sales totaled $12.4 billion and were up 5% year over year, showing resiliency despite inflation. And when excluding Veklury, its COVID-19 treatment, sales for all Gilead Science products have been up 7% over the past three quarters.

The company received great news before the end of 2022 with the Food and Drug Administration approving its twice-yearly injectable HIV treatment, lenacapavir, which the company will sell under the brand name Sunlenca. It will be available to patients who have limited treatment options "due to resistance, intolerance, or safety considerations." For long-term investors, that can drive even stronger results in the years ahead as analysts project that the treatment could generate up to $1.5 billion in peak annual sales.

Not only is the business consistent and reliable, but the stock also pays an above-average yield of 3.4% (the S&P 500 average is 1.7%) -- that's something it didn't offer during the Great Recession, as it only started issuing dividends in 2015.  That can be an additional motivation for investors to buy the stock, as it can provide some solid recurring revenue at a time of economic uncertainty.

With $9 billion in free cash flow generated over the past year, Gilead's in solid shape to continue paying its dividend, which was an outflow of just $3.7 billion during that time frame.

2. McDonald's

McDonald's can be a resilient stock to own in a downturn because its low-priced meals can offer consumers a way to eat without breaking their budgets. The fast food giant's dollar menu, in particular, can provide much more cost-effective options than eating at a sit-down restaurant.

The proof is also in its recent results. When the company last reported earnings in October, McDonald's reported that its comparable sales in the U.S. were up for the ninth consecutive quarter -- even as consumers battle above-average inflation. Globally, comparable sales were up 10% for the third quarter.

Like Gilead, McDonald's also offers an attractive dividend that yields 2.3%. And in light of its strong results, the company announced a rate hike of 10% to the dividend last year.

Given that inflation remains a problem for the economy, McDonald's could continue to do well this year, and potentially be an above-average investment to hold if a recession hits.

3. Walmart

Another resilient stock that investors may be able to count on this year is Walmart. Like McDonald's, its focus on offering low prices could make it an attractive option for cash-strapped consumers looking to tighten their budgets.

The company also has an advantage over rival Target in that groceries make up more than half of its revenue (versus just 20% for Target). That makes its business less dependent on big-ticket purchases, and, at the same time, makes it a more attractive one-stop-shopping option for consumers.

WMT Revenue (Quarterly YoY Growth) Chart

WMT Revenue (Quarterly YoY Growth) data by YCharts

The company's growth rate has been accelerating over the past year as proof that it is effectively attracting consumers, and that's a trend that may continue this year. 

The only thing that might prevent me from buying Walmart's stock right now is its high price-to-earnings multiple of 45, as it is battling with high inventory levels, as are other retailers. However, heading into a recession, Walmart is still an investment that could deliver above-average returns for investors just due to its sheer size and strength.