The S&P 500's performance in 2022 -- down more than 20% -- is a clear indicator of investor pessimism as we look ahead. A combination of factors, from soaring inflation, rising interest rates, the war in Ukraine, corporate layoffs, and still-struggling supply chains, are causing many to raise the alarm on the economy in the near term. 

If you're worried, like many other investors are right now, that a recession is on the way, then take a look at O'Reilly Automotive (ORLY 0.57%) as a stock to protect your portfolio and peace of mind. 

An all-weather business

The last economic downturn (besides the 2020 pandemic-induced one) that the U.S. faced was the Great Recession that started in Dec. 2007 and lasted until June of 2009. Caused by an overextension of subprime mortgages and a hot housing market, the S&P 500 shed 36% of its value during this roughly 18-month stretch. O'Reilly Automotive, on the other hand, produced a positive return of 14% over the same time period. 

The top after-market auto parts retailer posted revenue growth of 41.8% in 2008 and 35.5% in 2009, which are the highest annual gains the business has registered since 1998. Unsurprisingly, as many other companies were struggling throughout this difficult time, the fact that O'Reilly was actually thriving no doubt contributed to its stock performance. 

It all boils down to the company's business model, which produces solid results no matter what macroeconomic environment we're in. If the economy is in a recession, consumers are more likely to hold off purchasing new vehicles, and they will instead try to extend the lives of their existing cars by leaning on maintenance and repairs. Selling brakes, batteries, and motor oil, among other items, at its 5,811 stores in the U.S., O'Reilly benefits in this type of scenario. 

On the flip side, when we're in a robust and growing economy, consumers generally drive more and increase the wear and tear on their cars. O'Reilly's management team, led by CEO Greg Johnson, cites miles driven as one of the key factors that determines the success of the business. If consumer confidence is high, people are going to get out of their houses and drive more. Again, this situation supports demand for O'Reilly's products. From 2014 through 2019, during a time when the U.S. economy was expanding at a solid pace, O'Reilly's revenue grew at a compound annual rate of 7.1%. 

Is now the right time to buy? 

To help answer that question, let's look at recent results. In the first quarter of this year, O'Reilly increased sales 6.6% year over year. And this was facing a difficult comparison to Q1 2021, when the company grew revenue 24.8%. Same-store sales, a key measure for any retail-based business, rose 4.8% in the latest quarter. A low unemployment rate, coupled with elevated car prices, has led to success. 

Investors will appreciate the fact that O'Reilly is an extremely profitable enterprise. Not only did the business post a gross margin of 51.8% and an operating margin of 20.3% in Q1, the company produces lots of free cash flow, totaling $2.5 billion in 2021. Management has used this consistent excess cash to reduce the outstanding share count by almost half over the past decade. 

Trying to predict the macroeconomic environment is almost impossible. Consequently, it's best for investors to focus on what they can control, and that is to position their portfolios for whatever happens. O'Reilly outperformed the broader market during the Great Recession, and it's a good bet to do it again should another downturn happen. 

With the stock down 11% in 2022, O'Reilly shares now trade at a price-to-earnings ratio of just 20. While this is slightly more expensive than peers like AutoZone and Advance Auto Parts, it is lower than the company's historical five-year average of 21. What's more, O'Reilly's valuation today is in line with the S&P 500. For such an outstanding business, this seems like a no-brainer investment that should be worth more than the typical corporation out there.