Navigating the stock market has been tricky in 2022, with rising interest rates and declines in consumer spending bringing down the shares of numerous companies. The tech industry has been hit especially hard as some of the world's most valuable companies have experienced steep declines in their stock prices.

As a result, it has become crucial to invest in companies that have promising outlooks well into the future. After all, a long-term investment plan can help safeguard you from temporary economic declines, such as a potential recession in 2023. 

Microsoft (MSFT -1.41%) and Nvidia (NVDA -2.68%) have each suffered double-digit slides in their stocks since January but remain excellent buys for the long haul. Here's why. 

1. Microsoft 

Microsoft's stock has dipped 25% year to date, brought down primarily by macroeconomic headwinds. However, it has retained its position as a great long-term investment thanks to its 200% stock growth in the last five years. 

While 2022 hasn't been the easiest year for investors, it has highlighted the strength of certain companies' businesses that have continued to grow despite greater market declines. For example, in the first quarter of Microsoft's fiscal 2023, the company reported an 11% year-over-year rise in revenue of $50.1 billion while operating income grew 6% to $21.5 billion.  

Microsoft's growth primarily came from the diversity of its segments. Despite its "more personal computing" segment earning $13.3 billion in revenue, resulting in a 15% decrease in operating income of $4.2 billion, its other segments were able to pick up the slack. Microsoft's productivity and business processes segment, including revenue from its Office products and LinkedIn, grew by 9% to $16.4 billion, while its intelligent cloud segment increased by 20% to $20.3 billion. 

Microsoft's cloud computing business is especially promising for its long-term future, considering its platform Azure has a 21% market share in the industry, second only to Amazon Web Services' 34%. Microsoft ended September with $63.33 billion in free cash flow compared to Amazon's negative $26.32 billion. As a result, Microsoft is heading into the new calendar year well equipped to invest heavily in Azure and steal market share from Amazon to eventually dominate the $368 billion cloud computing market.

The lucrative industry is expected to see a compound annual growth rate of 15.7% until 2030, making Microsoft stock a no-brainer investment for the long haul. 

2. Nvidia 

The media has taken a doom-and-gloom attitude toward Nvidia this year, as its position as a leader in the PC industry has dragged its stock down 41% year to date. However, the 237% growth in its share price over the last five years has underlined the importance of holding a stock for the long term.

In Nvidia's latest quarter, revenue of $5.9 billion decreased by 17% year over year, with operating income of $601 million falling 77% since the year before. Declines in earnings primarily stemmed from a 51% drop in the company's gaming segment, which suffered a steep drop in consumer demand for its graphics processing units (GPUs). 

Despite a temporary decline in Nvidia's PC gaming segment, its data center business reported $3.83 billion in revenue, up 31% year over year. As the company's biggest earning segment, its data center growth is especially positive for Nvidia's long-term prospects. 

Worth $206.2 billion in 2021, the data center market is expected to grow at a compound annual growth rate of 10.2% until 2028, according to BlueWeave consulting. Nvidia's significant revenue rise in its data center segment confirms the industry's rapid growth and the likelihood of the company enjoying substantial gains for years to come.

Additionally, Nvidia's recent partnership with Microsoft to build a "massive cloud AI computer" will further expand its data center business and put the company in an excellent position as it works closely with one of the leaders in cloud computing.

Over the long term, Nvidia will likely overcome economic declines with a return to growth in its PC gaming business and an exceedingly profitable data center segment. The company has had a challenging 12 months, but Nvidia shares remain a buy for investors willing to hold for the long haul.