When it comes to online search, there's Google and then there's everyone else. When it comes to online retail, there's Amazon.com (AMZN -1.64%) and then there's everyone else. But when you put the two dominant companies head to head, it may be a tough call on which one makes a better buy for investors.

Both Amazon and Alphabet (GOOG 0.74%) (GOOGL 0.55%) stocks have meaningfully outperformed the S&P 500 year to date. But investors can't dwell on the past; they have to look to the future to determine where to invest. Let's look at how these two companies' prospects stack up, to determine which is a better buy.

Google sign in Sydney offices.

Image source: Google.

Google is one of two dominant forces

The vast majority of Alphabet's revenue and practically all of its profits come from Google. And nearly all of Google's revenue comes from advertising. Google's ad revenue continues to climb at a steady pace, up 18% year over year, benefiting from the megatrend of the shift from television advertising to digital advertising.

But Google isn't the only force in advertising. Facebook (META -0.52%) is still growing rapidly, with ad revenue up 47% year over year last quarter. Combined, Facebook and Google account for nearly all the revenue growth in digital advertising.

Facebook is investing heavily in video, and its new Watch tab could eventually have a negative impact on YouTube. Meanwhile, Amazon is taking away product search queries from its search engine, which are some of the most valuable searches for advertising.

That said, Google is still very much a dominant force in advertising. It has seven products with over 1 billion users -- Search, Gmail, Google Maps, YouTube, Google Play, Chrome, and Android. Not even Facebook can make the same claim (yet). That kind of broad engagement gives Google an unparalleled data set to produce strong returns on investment for its ad buyers. That's a huge competitive advantage that's almost impossible for smaller competitors to overcome. It explains why Google and Facebook are capturing all of the growth in digital advertising.

Worker placing Amazon box on a conveyor belt.

Image source: Amazon.com.

Amazon is the dominant force

Amazon's moat in online retail is even bigger than Google's moat in digital advertising. Amazon's Prime program makes customers extremely loyal, attracts tons of third-party merchants to its marketplace, and helps fund Amazon's investments in fulfillment capacity and technology. As a result, Amazon is the first place the majority of consumers go to when searching for a product online.

While Wal-Mart (WMT 1.32%) has taken steps to compete online and is starting to attract third-party merchants to its online marketplace as well, it was never able to make customers loyal through a membership program.

Amazon Prime now has around 80 million members in the United States and a growing number of international subscribers as it expands to more countries. It rolled out Prime Video globally at the end of last year, establishing the Prime brand in more countries and setting itself up for full Prime benefits in more markets in the future.

The acquisition of Whole Foods provides another way to convince customers to join Prime. It could also help upsell customers to its grocery delivery service AmazonFresh. And it also provides another avenue to sell its hardware like Echo and Kindle.

No other retailer can compete with Prime and the loyalty it instills in Amazon's customers. That gives it a huge competitive advantage.

I'd rather buy Amazon

To be sure, both Alphabet and Amazon present excellent investment opportunities and capitalize on several megatrends. But Amazon's competitive advantages appear stronger than Google's, which faces serious competition for ad dollars from Facebook and could see increased competition for attention (and ad display opportunities) in the future. Amazon has lots of competition as well, but it's created a virtuous cycle with Prime.

It's hard to judge Amazon's valuation using metrics such as earnings or EBITDA because of the constant investments management makes to grow the business in various avenues. Nonetheless, I believe the growth of Amazon Web Services (its cloud computing division), Prime, AmazonFresh, and other subscription revenue, scale of its international operations, and further scale in its fulfillment center capacity ought to push operating margin higher.

While Amazon might not produce a profit margin near the same level as Alphabet, it could produce operating margin on par with Wal-Mart (about 6%). For reference, last year Amazon's operating margin was 3.1%. But it was 5% in North America. As operating margin expands and revenue continues to grow, that money will eventually find its way to the bottom line and produce huge earnings-per-share growth for Amazon and its investors.