It hasn't been easy to be a stock investor in 2022, with a sell-off bringing down the shares of some of the world's most valuable companies. However, the market downturn has essentially put numerous stocks on sale, making now a great time to invest ahead of the new year.

Microsoft (MSFT -0.25%) and Apple (AAPL 1.30%) have watched their stocks tumble over the last year. Despite these declines, each company has proven the strength and resilience of their businesses amid poor economic conditions. This makes them excellent long-term investments. 

But if you only have room to add one more company to your portfolio, you might wonder whether Microsoft or Apple's stock is the better buy. Let's find out. 

Microsoft demonstrates long-term potential with Azure

Microsoft shares have fallen 29% year to date, as declines in the PC market have concerned investors. However, the company has more than proven its value as a growth stock, with its shares still up 177% over the last five years. 

The tech giant has come a long way since its founding in 1975, branching out into multiple industries, making the company's primary strength its diversity. As the home to potent brands such as Windows, Office, Xbox, LinkedIn, and Azure, Microsoft has developed considerable market shares in different industries and safeguarded itself against an economic downturn.

In the first quarter of its fiscal 2023, Microsoft's revenue climbed 11% year over year to $50.1 billion, while operating income rose 6% to $21.5 billion. The company reported growth despite its PC-centered segment seeing a slight decline in revenue and a 15% fall in operating income of $4.2 billion.

Microsoft's growth in an economically challenging quarter was primarily due to its other high-performing segments being less affected by macroeconomic headwinds. For instance, the company's cloud-computing business saw revenue increase 20% year over year to $20.3 billion, with operating income rising 17% to $8.9 billion. Meanwhile, Microsoft's productivity and business-processes segment had a revenue increase of 9% to $16.4 billion, and operating income reached $8.3 billion, rising 10%.

The Windows company's performance in 2022 bodes well for its long-term future, as its investment across different industries has clearly paid off and made it a reliable investment. The $368.97 billion cloud-computing industry alone will see a compound annual growth rate (CAGR) of 15.7% until 2030, with Azure's 21% market share likely to continue providing significant gains for years.

Along with solid positions in gaming, operating systems, enterprise resource-planning software, and more, Microsoft is an excellent stock pick. 

Apple's services segment continues to grow

Apple shares have dipped 28% year to date, with concerns over its dependency on China for its iPhone production dragging it down almost 10% over the last month. However, the company remains one of the best investments out there, with consistent demand for its products no matter the economic climate and a promising services business. In fact, Apple's stock has retained its growth of 211% over the last five years despite a challenging year. 

If the sell-off in 2022 has taught us anything, it is the importance of investing in stocks with a long-term mindset. Investors have grown uneasy over Apple's iPhone business since November, with the smartphones making up 52% of its revenue in its fiscal 2022 and COVID-19 restrictions in China putting strains on production. However, I wouldn't bet against the cash-rich company throwing everything it can at improving its production line as soon as possible. 

Apple is already manufacturing a portion of its iPhone 14s in India, with JPMorgan Chase estimating that about 25% of all the company's products will be produced there by 2025. The move out of China will no doubt be lengthy, but that doesn't dampen its potential over the long term.

Moreover, Apple's services business is booming and has quickly become the company's second-biggest earning segment. In its fiscal 2022, services revenue rose 14% year over year to $78.1 billion. By contrast, iPhone revenue increased by 7% during the year. Services also offers attractive profit margins, with earnings hitting 71.7% in 2022, while products reported a 36.3% profit margin. The swift growth of services is another reason to bet on Apple, with the segment able to take some pressure off its iPhone business as it moves out of China. 

As some of the most profitable businesses in the world, a compelling argument could be made for either Microsoft or Apple's stock. When comparing price-to-earnings ratios, Microsoft's sits at 25.53, while Apple's is a marginally preferable 21.28. Meanwhile, Microsoft's free cash flow was $63.3 billion as of Sept. 30, while Apple's was $111.4 billion. 

At its current metrics, Apple seems to offer investors slightly more value, making it the better buy. However, regular investments in both companies could yield significant returns over the long term.