Despite its positive performance over the past month, the S&P 500 is still down 13% in 2022. After peaking around the start of the year, the broad stock index has trended lower thanks to macroeconomic concerns, mainly around the ongoing soaring inflation. A surge in consumer demand following the depths of the pandemic, coupled with struggling supply chains, has led to rising prices across the economy. 

Enter the Federal Reserve, which is now tasked with raising interest rates to curb inflation, but without putting the U.S. economy in a full-on recession. If this challenge can be achieved, a so-called soft landing, then consumers and businesses would certainly be happy. If not, however, then the markets could be in for a far worse situation. 

In that case, investors should be prepared. Here are three stocks to buy during a recession. 

1. Chipotle Mexican Grill 

First on this list is Chipotle Mexican Grill (CMG 0.17%). The popular Tex-Mex restaurant chain just reported another stellar quarter, with second-quarter revenue rising 17%, same-store sales jumping 10.1%, and adjusted earnings per share increasing 24.7% compared to Q2 2021. Shares immediately rose 8% after the earnings announcement, and they are up 359% over the past five years. 

Investors are right to wonder why Chipotle is on this list. After all, consumers could easily trade down to cheaper fast-food alternatives, like McDonald's and Wendy's, when times are tough. But consider the company's focus. "The majority of our customers are a higher household income consumer," CEO Brian Niccol highlighted on the Q2 earnings call. "And we've actually seen their frequency increase."

This favorable situation helps insulate the business in a difficult economic environment. Niccol did admit, however, that the low-income consumer has pulled back somewhat. 

While many other restaurant chains struggled during the depths of the pandemic, Chipotle absolutely thrived, opening stores aggressively and growing sales and profits at a rapid pace. In a recessionary scenario, the same situation is likely to play out. Therefore, investors would be wise to consider adding this outstanding business to their portfolios. 

2. Costco Wholesale

The next stock that investors should consider buying in a recession is warehouse club operator Costco Wholesale (COST -0.55%). With 835 locations worldwide, this business is a consumer favorite thanks to its wide assortment of merchandise, ranging from electronics and groceries to clothing and appliances, at extremely low prices. Trailing 12-month sales totaled $217.5 billion, making Costco the world's third-largest retailer. 

Thanks to consumers flocking to its stores as a one-stop shop, Costco was a clear winner during the pandemic, and this momentum has yet to fade. In the month of July, same-store sales in the U.S. increased 11.4%, compared to a 13.1% rise in the year-ago period.  

Costco is able to offer such low prices, something invaluable in a recession, because of its no-frills shopping environment and massive scale. The company is able to negotiate favorable terms with suppliers, who are happy to do business with Costco because of its huge scale and distribution. This resulted in an intentionally thin operating margin of 3.4% in the most recent quarter.

"There's a lot of discussion and talk about a recession coming, but if you look in our buildings and if you've been on an airplane lately, you'd never notice it," Bob Nelson, senior vice president of finance and investor relations, mentioned on the latest earnings call. 

Despite its expensive price-to-earnings ratio of 43, Costco is a business you want to own during a weak macroeconomic period. 

3. O'Reilly Automotive 

The last stock on this list is probably the most well-positioned to flourish in a recession. And that's because leading aftermarket auto parts retailer O'Reilly Automotive (ORLY 0.57%) has done just that. During the Great Recession, the business posted year-over-year revenue gains of 41.8% in 2008 and 35.5% in 2009, which would be exceptional gains for any company. 

When the economy is roaring and times are good, O'Reilly also does very well. That's because with low unemployment and high consumer confidence, people tend to drive more. And this increases the wear and tear on their vehicles. O'Reilly's 5,873 domestic stores are ready to serve both DIY and professional mechanics with brakes, motor oil, batteries, and other items to help take care of repairs and maintenance. 

On the other hand, recessions are where O'Reilly really shines. In this scenario, consumers hold off on buying new cars, and instead they focus on extending the lives of their existing vehicles. The result, again, is a boon for O'Reilly's business, as demand for the products it sells skyrockets. 

O'Reilly might not be as exciting as popular tech growth stocks, but over the past five years, is has produced a return of 244%, trouncing the S&P 500's total return of 84% during the same time. Buying this stock is a no-brainer decision for investors worried about a recession.