The Great Recession took place over a decade ago and was a tumultuous time for investors who suffered significant losses. Some stocks even performed much worse than the S&P 500 while others proved to be resilient during this stretch.

Below, I'll look at a couple of stocks that outperformed the market and a couple that crashed. Are any of them safe options to invest in right now, with a possible recession looming later this year?

Walmart -- up by 10%

Walmart (WMT -0.08%) performed the best of the stocks on this list during the Great Recession as its share price was up around 10% during that time frame. Despite the downturn in the economy, the business showed solid growth.

For its fiscal year that ended on Jan. 31, 2009, Walmart's sales totaled $401 billion and rose an impressive 7.2%. There were increases in Sam's Club, its international segment, and Walmart U.S. The company was coming off a record-setting performance in the quarter as its operations were proving to be resilient.

Fast forward to today, and the business remains solid. In its most recent fiscal year (ended Jan. 31, 2023), sales of $611.3 billion were up 6.7%. If you exclude divestitures, the growth rate would be up around 7.4%.

Walmart has been a steadily growing business over the years and could continue to be a relatively safe place to invest your money if the economy falls into another recession.

Walgreens Boots Alliance -- down by 25%

Retail pharmacy Walgreens Boots Alliance (WBA 0.34%) can provide a relatively stable investment during a downturn because consumers rely on its stores day in and day out. Whether it's to buy pharmaceuticals or day-to-day essentials, the company's stores are conveniently located within five miles of most of the U.S. population. During the early stages of the pandemic, it was also an easy place to obtain a vaccine.

During the Great Recession, the stock's losses were around 25%. While that's not great, it's still better than the S&P 500's losses of close to 40%.

For the year ended Aug. 31, 2009, the company reported record revenue of $63.3 billion, which grew at a rate of 7.3% from the previous year. Prescription sales were a big part of that result, rising by 9% for the final quarter of the year and accounting for two-thirds of the company's top line.

Since then, however, the business has changed as Walgreens acquired European pharmacy chain Alliance Boots and more recently has been pivoting to healthcare. The latter move makes it a particularly riskier business to invest in as Walgreens is devoting more cash and resources to expansion.

While the healthcare stock outperformed the market during the Great Recession, there's no guarantee it will be able to do so if there's another recession in the near future.

Williams-Sonoma -- down by 60%

Specialty retailer Williams-Sonoma (WSM 0.27%) is susceptible to downturns in the economy as its business success is more dependent on discretionary spending. While Walmart and Walgreens may provide consumers with essentials, those same consumers aren't as likely to spend money on high-quality products at Pottery Barn in tougher times.

In the fiscal year ended Feb. 1, 2009, amid the recession, the company's net revenue of $3.4 billion was down 15% from the previous year. And even a year later, sales would fall another 8% to just over $3.1 billion.

During challenging economic periods, Williams-Sonoma can be a risky stock to hold. In the long run, however, given the stock's low valuation, it could result in good profits if you're willing to buy and hold.

Nvidia -- down by 68%

The worst performance on this list belongs to chipmaker Nvidia (NVDA 0.86%). As with high-end products, technology is another area where businesses can often scale back on spending or delay it outright for the sake of keeping costs down.

Nvidia's sales for the year ended Jan. 25, 2009 totaled $3.4 billion and were down 16% from 2008. And a year later, sales would fall again to $3.3 billion as Nvidia noted declining demand in its professional solutions business, saying that there was a "slow recovery of corporate spending following the economic recession that began during fiscal year 2009."

Nvidia's business is bigger now, and the growing need for computer chips ensures that this can be a solid growth stock to own for the long haul. But investors should brace themselves for the possibility of a bumpy ride in the next year or two if a recession takes place.