While there are no guarantees in the stock market, sticking with growing companies that are leaders in their markets is one of the safest ways to choose stocks for the long term.

ServiceNow (NOW 2.65%) and Google parent Alphabet (GOOG 0.92%) (GOOGL 0.93%) are two leading tech companies producing stellar financial results that are sending their share prices to new highs. Here's why these stocks are smart buys for 2024 and beyond.

1. ServiceNow

Companies are always looking for ways to improve employee productivity, and they are increasingly looking at generative artificial intelligence (AI) as the answer in 2024. This is a big opportunity for the leading digital workflow platform ServiceNow.

The stock has delivered a cumulative return of 1,400% over the last 10 years, which was driven by average annual revenue growth of 35%. The good news for new investors is the company is still growing revenue at impressive rates. In the most recent quarter, subscription revenue grew 24% year over year, with generative AI solutions playing a key role in securing top deals with other companies.

A great advantage for ServiceNow is its partnership with Microsoft to integrate Now Assist AI with Microsoft Copilot. It shows why ServiceNow is the gold standard in the digital workflow market. Most of ServiceNow's biggest deals in the quarter were for generative AI products.

Importantly, subscription-based businesses make for solid long-term investments because of their recurring revenue streams and high margins. This explains why analysts expect the company's earnings per share to grow faster than revenue at an annualized rate of 29% in the coming years. That is enough growth to potentially double your investment in five years or so, even if investors are paying a lower price-to-earnings multiple for the stock down the road, which is currently steep at 77 times trailing earnings.

2. Alphabet (Google)

AI will have a lasting impact on the economy, and nowhere will that be more evident than how people shop and find information online. This is why investors are increasingly looking at Alphabet as a major beneficiary of this revolutionary technology. Google has been investing in AI for many years, and it's starting to pay off.

Revenue and earnings grew 15% and 61% year over year in the first quarter. The company is delivering these impressive results while investing in its Gemini AI model to bring smarter search results and other features to billions of users that rely on Google products.

It's also seeing strong demand for AI services from enterprise customers in Google Cloud, which is turning into a profitable revenue source for Alphabet. Google Cloud hauled in $900 million of operating profit last quarter. That is up from just $191 million in the year-ago quarter. The company says its new AI models and algorithms are more than 100 times more efficient than they were just 18 months ago.

The stock has soared to new highs this year, but still trades at a reasonable valuation. Analysts expect Alphabet to grow earnings at an annualized rate of about 19% over the next several years. At a forward P/E of 22, investors can expect the stock to deliver returns consistent with its future earnings growth. That could mean a double in the next five years, if not more, depending on how well Google monetizes AI features across Search, YouTube, and Google Cloud.