Both Amazon.com (NASDAQ:AMZN) and Google's parent company, Alphabet (NASDAQ:GOOG) (NASDAQ:GOOGL), are trading near their all-time highs. Amazon's stock has climbed so much in the past 12 months, it's made its founder and CEO Jeff Bezos the second wealthiest person in the world, recently surpassing Warren Buffett and Amancio Ortega. Alphabet shares are up a modest 11%, compared with Amazon's 51% increase.

But investors can't dwell too much on the past. They need to look to the future to determine which stocks offer the best opportunities for further gains. Today we'll compare the two tech giants head to head to determine which is a better buy: Amazon.com or Alphabet.

An Amazon worker watching boxes move down a conveyor belt.

Image source: Amazon.com.

Amazon is firing on all cylinders

Amazon's core retail business is strong. It generated $124 billion in revenue in 2016 across its North American and international retail segments. That's up 25% from $99 billion in 2015. Amazon's growth is fueled by increasing Prime membership, which acts as a huge incentive for both customers and merchants to use Amazon's marketplace before looking elsewhere. A survey from BloomReach found that 55% of online shoppers begin their product search on Amazon.com.

But what's really driving Amazon's stock price higher is its more profitable operating segment, Amazon Web Services. AWS is Amazon's cloud computing service, which supports all sorts of web applications, from basic websites to huge data-consuming apps such as Netflix (NASDAQ:NFLX). AWS sales increased 55% last year to $12.2 billion. More importantly, the segment's operating profit of $3.1 billion is about three times as much as Amazon's retail business.

Amazon is also expanding into other high-margin businesses. Its advertising business is growing rapidly, albeit on a small base. Morgan Stanley analyst Brian Nowack estimates that Amazon's ad business could grow to $5 billion by next year and $7 billion by 2020. And those sales are high margin, which means that despite the relatively small revenue compared with the rest of Amazon's retail segment, Amazon could see significant profit growth. Nowack notes, however, that Amazon's ad revenue growth shouldn't cut into Google's core search ad sales.

Alphabet is as steady as ever

Alphabet may have changed its name to reflect its other bets besides Google, but the online advertising business is still its core business. Google accounted for 99% of the company's revenue and 118% of its operating income in 2016, while "other bets" produced an operating loss.

Scooters lined up in Google's offices.

Image source: Google

Google is managing to expand its already dominant share of the search advertising market. eMarketer expects Google to expand its share by 16.1% to claim roughly 78% of the U.S. search market this year.

Google growth is being fueled by its Android operating system, which recently became the most popular operating system across all devices. Google is also extremely popular across other mobile OSes, and consumers are turning to their smartphones more often to look up details on just about anything. That means more Google searches and more search ad revenue.

YouTube is also a dominant force on mobile, with the average mobile viewer spending 40 minutes watching video per session as of mid-2015. YouTube is currently under scrutiny for displaying ads before objectionable content, but Google has quickly taken steps to counteract the issues. The negative impact on Google should be relatively small.

Lots of growth, but which should you buy?

The growth outlook for both Amazon and Alphabet is quite strong, so how can investors decide which to buy?

Both companies have strong balance sheets with huge amounts of cash and relatively little debt. Alphabet's $86 billion in cash and equivalents, however, completely dwarfs Amazon's $26 billion. Alphabet even has less long-term debt than Amazon. Alphabet also produces more cash from operations -- $36 billion, versus $16 billion. That's not to say Amazon's struggling for cash, but Alphabet is in a better position.

Perhaps more telling are the valuation multiples investors are putting on both companies. Amazon currently trades for a price-to-earnings multiple of 182.6. Alphabet (C shares) is comparatively cheap at 29.6 times earnings.

But analysts are significantly more bullish on Amazon's earnings growth prospects, expecting an average of 72.7% earnings growth over the next five years, compared with just 19% for Alphabet. 

Google's growth is coming from the megatrend of advertisers who are shifting more ad dollars to digital while consumers spend more time searching and watching videos on their smartphones. Amazon's growth will come from AWS and the emergence of other high-margin categories such as advertising. Amazon is also working off a much smaller net-income base than Alphabet. Its growth is much less certain than Alphabet's, but it has the potential to pay off in the long run.

If you can stomach the up-and-down nature of Amazon stock, I think it makes a better long-term buy despite its sky-high valuation. For more conservative growth investors, Alphabet is an excellent option. 

Suzanne Frey, an executive at Alphabet, is a member of The Motley Fool's board of directors. Adam Levy owns shares of Amazon. The Motley Fool owns shares of and recommends Alphabet (A shares), Alphabet (C shares), Amazon, and Netflix. The Motley Fool has a disclosure policy.