The Nasdaq Composite surged 43% in 2023, and one of the big investing themes driving investor enthusiasm was the booming demand for artificial intelligence (AI) technology. The IDC projects spending on AI software to grow 31% per year and reach $251 billion annually by 2027.

The best thing about the AI opportunity is that you don't have to make risky investments to do well. Microsoft (MSFT 0.59%) and Google parent Alphabet (GOOG -0.75%) (GOOGL -0.77%) are tech juggernauts that have been investing in AI for years and are already seeing the fruits of those efforts. Both stocks saw their prices more than double over the last five years and beat the market again in 2023.

Let's compare these tech leaders to see which is the best AI stock to buy right now.

Microsoft

Microsoft is a widely recognized brand, which will benefit the company as it introduces new AI services for businesses and consumers. Over the last year, it introduced Bing AI and Copilot for Microsoft 365 on the consumer side. These offerings could attract more customers and solidify Microsoft's dominant market share in the PC market.

As part of its investment in OpenAI, Microsoft has started to introduce AI features for the Bing search engine and a Windows with Copilot preview. Copilot could add a valuable new revenue stream, with Microsoft charging a small subscription fee to use it with Office in Microsoft 365.

Microsoft was very successful in turning its productivity software into a cloud subscription service. AI should give the company more ways to monetize its software, and it's already doing just fine. In the last quarter, Microsoft said its productivity segment, including Office, LinkedIn, and Dynamics products, brought in $18 billion in revenue, up 13% year over year.

Microsoft can also monetize AI through the Azure enterprise cloud service business. This unit has been steadily gaining market share in cloud computing, and it is currently No. 2 behind Amazon Web Services. Azure grew revenue by 29% year over year in the September-ending quarter, with management saying that demand for AI services contributed a few percentage points to growth.

Altogether, Microsoft expects AI to eventually bring in $10 billion in annual revenue, which would make it the fastest business to reach that level of revenue in company history.

Over the last 10 years, Microsoft's revenue and earnings grew 10% and 14% per year, respectively. AI technology is extending Microsoft's runway of growth and giving the company more ways to build valuable recurring revenue streams through subscriptions.

Alphabet

The Google parent has been investing in AI for several years. Billions of people use Gmail, YouTube, and other Google services, which has made Alphabet the leading digital advertiser. The company's investments in AI will make it a major influencer in how people engage with this technology in the future.

Alphabet may not directly monetize certain AI features to generate more revenue like Microsoft. But the company rolled out a feature last year in Workspace Labs that lets Gmail users draft an entire email from a simple prompt using AI. New features like this could go a long way towards maintaining a large base of users and strengthening Google's brand.

Alphabet made a big splash in AI last year by launching Google Bard, a conversational search engine powered by generative AI. In December it introduced Gemini, the company's most powerful AI model, which outperforms human experts on massive multitask language understanding (MMLU).

The company says that "millions of people are now using generative AI across our products to do things they couldn't even a year ago." Because people already use Google search and YouTube for researching products when planning to buy something, smarter search powered by AI should drive up usage of these products, and that suggests more opportunities for advertising -- the company's largest revenue source.

Alphabet controls about a quarter of the digital advertising market. This fueled its growth over the last 10 years, where revenue and earnings increased at high annual rates of 20% and 19%, respectively.

Following a sluggish performance in 2022 as a result of a weak advertising market, Alphabet is starting to build momentum again, with analysts expecting double-digit growth in revenue for 2024.

Which stock is the better buy?

Both of these companies are similar in terms of the number of people that rely on their services and their dominant market shares in software and online search. But since 2019, Alphabet has grown revenue faster than Microsoft, and analysts project more double-digit growth for the search leader.

MSFT Revenue (Quarterly) Chart

Data by YCharts

Analysts expect Alphabet to grow earnings by 20% per year over the next five years, compared to 15% for Microsoft. Note that these estimates are consistent with each company's previous 10-year track record.

Moreover, investors can buy shares of Alphabet at a lower forward P/E of 20, compared to 33 for Microsoft. Investors can get more growth at a cheaper price with Alphabet, which makes the Google owner the better buy at the moment.