The definition of a recession may vary, but one standard is when gross domestic product (GDP) falls for two consecutive quarters. This year, it fell in the second quarter, so the possibility exists that we may already be headed toward a recession.

Investors need to know how to protect their investments when a recession starts because it generally means less consumer spending, and that often leads to less revenue for companies. Recessions vary, and there's no such thing as a "recession-proof" stock. But certain sectors are more recession-resistant, such as consumer staples and healthcare. That's because both sectors involve less discretionary spending than travel, dining, or entertainment.

That brings us to two huge healthcare stocks investors often look at as safe havens in times of economic uncertainty: Johnson & Johnson (JNJ 0.29%) and UnitedHealth Group (UNH -1.03%).

1. J&J has stood the test of time

Johnson & Johnson is a huge healthcare company with 142,000 employees. It reported $24.3 billion in revenue in the second quarter, up 3%, year over year. So far this year, J&J stock is down about 7%, while the S&P 500 Index has slumped 25%.

During the Great Recession from December 2007 to June 2009, the S&P 500 Index experienced a total return of -41.5% versus -12.5% for Johnson & Johnson. Why did it hold up so well? Chalk it up to one awkward phrase -- "diversified inelastic spending."

Johnson & Johnson makes a lot of products, drugs, and medical devices, but with the exception of a few beauty products, much of what the company makes isn't exactly discretionary spending.

JNJ Revenue (Quarterly) Chart

Data Source: JNJ Revenue (Quarterly) data by YCharts

The company operates in three segments: pharmaceuticals, such as the drugs people take every day to preserve their health; consumer healthcare, which includes staples such as Band-Aids, Tylenol, and Zyrtec to treat allergies; and medtech, which includes things such as artificial hips, knees, and medical equipment such as sutures and hemostats.

During the years from 2007 to 2009, Johnson & Johnson's earnings per share (EPS) went up 18.29%, in contrast to a lot of other companies.

There's another factor why investors didn't leave the company during the Great Recession -- the company is a Dividend King that has raised its payout for 60 consecutive years, including a 6.6% boost this year to $1.13 per quarterly share, giving it a yield of around 2.78%. That dividend gives investors a little more patience to hold on to a stock that declines because they can count on a dividend in the meantime.

Johnson & Johnson is in the process of spinning off its least-profitable division, consumer health, and plans to have that finished by sometime in 2023. While this leaves the company with less diversity, it also improves the company's profit margins. For example, in the second quarter, consumer health revenue was down 1.3%, year-over-year, while pharmaceutical was up 6.7% and medtech was down 1.1%.

2. UnitedHealth is riding a trend

UnitedHealth Group, a health insurance and managed healthcare company, has been up more than 3% so far this year. It's even bigger than Johnson & Johnson, with 360,000 employees, and it is the largest health insurer in the country, serving 280,000 people in the second quarter.

For the most part, health insurance isn't something people can afford to go without, making it somewhat recession-proof. However, when a recession hits hard enough that people lose their jobs, they lose their health insurance as well. During the Great Recession, the company's total return level fell by 53%.

UNH Chart Data Source: UNH data by YCharts

Fortunately, a greater part of UnitedHealth's business is now with Medicare and Medicare Advantage customers who are no longer on a company's insurance plan.

In the second quarter, UnitedHealth reported $80.3 billion in revenue, up 13% year over year. Its Optum managed healthcare division and its UnitedHealthcare insurance division both showed double-digit gains in revenue. The company also reported EPS of $5.34, compared to $4.46 in the same period last year.

The company also raised its quarterly dividend by 13.7% to $1.65 per share this year, giving its dividend a yield of around 1.27%. While UnitedHealth isn't a Dividend Aristocrat, it has grown its dividend by 676% over the past decade and has raised its dividend for 14 consecutive years.

The company should also benefit from a 10-year collaboration it recently announced with Walmart to offer healthcare to seniors and Medicare beneficiaries, as well as virtual care for patients of all ages. The agreement will begin with 15 Walmart locations in Florida and Georgia before expanding into new markets.

Not a difficult choice

UnitedHealth has come a long way and appears more recession-proof than it used to be, thanks to its increased Optum business and greater emphasis on Medicare customers. Yet, despite its run this year, it is a riskier bet if we're heading into a recession.

Right now, Johnson & Johnson still seems the safer choice of the two stocks, especially as a hedge against a recession, based on how it handled the Great Recession along with its more generous dividend.