Technology investors enjoyed monster gains during 2020 and 2021, even during the height of a once-in-a-century pandemic. The most innovative companies in the U.S. helped pull humanity through that challenging time and were aptly rewarded. But the stock market is giving back some of those gains in 2022 as the economic environment shifts to one with higher inflation and rising interest rates.

As a result, the Nasdaq-100 technology index is in a bear market with a year-to-date loss of 24%. But history is proof that a down market can be the best time to invest, because a recovery to new highs typically follows, broadly speaking, given enough time.

The key is to focus on quality stocks. Apple (AAPL 1.27%) and Microsoft (MSFT 0.37%) are already the two largest listed companies in the U.S., boasting market valuations of $2.5 trillion and $1.9 trillion, respectively, but they might also be among the best performers in the future. Here's why.

The case for Apple

During volatile stock market conditions, looking to some of the world's greatest investors for guidance is one way to navigate the uncertainty. Warren Buffett sits atop most people's list of investing geniuses, and as of this writing, Apple stock makes up 42% of his Berkshire Hathaway investment company's portfolio (by value).

Apple is a remarkable, culture-shifting organization. It turned its iPhone smartphone device from a luxury good to a must-have for many consumers, and it has complemented it with a range of top-selling accessories like the Apple Watch and AirPods wireless headphones -- both of which generate billions of dollars in sales every year. But the company has also built a lucrative ecosystem of services that seamlessly integrate with its devices, and that might be the most exciting part for investors.

Apple is home to Apple News, Apple Music, Apple TV+, and Apple Pay, which recently expanded to include Apple Pay Later. While services only accounted for 23.6% of the company's total $82.9 billion in revenue during the recent third quarter of fiscal 2022 (ended June 25), the upside is that they hold a much higher gross profit margin of 71%, compared to 52% for its hardware business.

Plus services revenue drove the company's growth in the quarter, expanding by 12% compared to last year, whereas hardware revenue was flat -- though it'll likely pick up after September once Apple unveils the next-generation iPhone.

Even as consumers tighten their belts amid a slowing economy, they're still willing to devote some of their income to Apple's products and ecosystem. That's one reason investors have been relatively hesitant to sell Apple stock this year; it's down about 10% in 2022, which is less than half the loss of the Nasdaq-100 index. Another reason is the enormous amount of money the company is returning to shareholders.

On top of its 0.6% annual dividend yield, Apple has repurchased more than $64 billion worth of its own stock so far in fiscal 2022, and it recently increased the total program by $90 billion. It ticks just about every box for an investing veteran like Buffett, especially in this challenging market.

The case for Microsoft

Microsoft and Apple were fierce rivals in the 1980s and 1990s. It's rare for both parties in a brutal corporate war to come out on top, but decades later, they're the two largest companies in the world. Their businesses have moved in slightly different directions more recently, but they still compete in some areas like hardware, which could become a battleground as new technologies like virtual reality grow in popularity.

Microsoft operates three main business segments, the largest of which is intelligent cloud computing. It's driven by Azure, a cloud services platform geared toward helping businesses migrate their operations online through hundreds of solutions like data storage and even machine learning applications. This is an area in which Apple doesn't even operate, which highlights how much the tech landscape has changed over recent decades.

Azure grew at a lightning-fast pace of 45% in the fiscal 2022 full year (ended June 30). It outpaced the intelligent cloud business unit overall, which expanded by a lesser 25%, but its large size is becoming a hindering factor with $75 billion in revenue. As that number climbs, it'll be more difficult to maintain rapid growth rates. But it's worth noting that the cloud computing opportunity could be worth $1.5 trillion every year by 2030, according to an estimate by Grand View Research, and Azure is already the second-largest player in the industry.

Microsoft is also expanding its presence in gaming, building upon its flagship Xbox platform to deliver new services like Xbox Cloud Gaming, which allows users to access their favorite console titles online and eliminates the need to download updates and patches. The cloud gaming industry is still in its infancy with an estimated value of $3.2 billion this year, but that could explode by more than 43% per year until 2029 to become an annual opportunity of $40.8 billion.

Microsoft has positioned itself to prosper over the long term by building a presence in industries of the future, and owning its stock allows investors to own a cross-section of the rapidly expanding digital economy.