For most of the past five years, Apple (AAPL -2.05%) has been the most valuable publicly traded company in the world. While the iPhone maker has lost some of its momentum in the past year, Apple still sports a market capitalization of about $600 billion.

However, investors seem to be betting on a changing of the guard in the coming years. Despite a sell-off following its recent earnings report, shares of Amazon.com (AMZN 0.12%) have quadrupled since the beginning of 2012. Its market cap of roughly $375 billion makes it one of the five most valuable companies in the world.

AAPL Market Cap Chart

Apple vs. Amazon.com market capitalization. Data by YCharts.

Given that Amazon.com is reinvesting essentially all of its earnings, while Apple is returning the vast majority of its cash flow to shareholders, Amazon's surging market cap implies that investors expect it to become the most valuable company in the world within the next decade.

Heavy investment continues at Amazon.com

Amazon stock has surged in the past two years as the company finally started to churn out steady profit increases, helped by the breathtaking growth of Amazon Web Services and the maturing of investments in Amazon's retail business.

In the first half of 2016, Amazon's operating income more than tripled to $2.36 billion -- up from $719 million a year earlier -- on a 30% jump in revenue. This strong performance powered the stock's most recent surge, from around $600 in late April to an all-time high of $847.21 last month.

However, Amazon's Q3 results brought a dose of reality to the most ardent bulls. Revenue is still growing quickly, but costs also ticked up significantly last quarter. As a result, operating income rose at a more sedate (albeit still enviable) 42% pace. This caused earnings to fall short of analysts' expectations.

On Amazon's earnings call, CFO Brian Olsavsky noted that the company has opened an extraordinary number of new fulfillment centers in the past few months: 18 during Q3 and five more during October. In addition, Amazon is spending heavily to add new streaming video content for Prime subscribers and to market its original series.

Amazon is adding lots of fulfillment centers in order to deliver packages faster. Image source: The Motley Fool.

These recent investments are expected to continue weighing on profitability in Q4 and perhaps in 2017 as well. Olsavsky wasn't specific about the pace of future investments -- Amazon tends to be quite opportunistic, so its profit trajectory has been and will continue to be "lumpy."

Two different market-cap trajectories

While Apple and Amazon.com are both megacap stocks, they couldn't be more different in terms of their cash flow profiles. (In theory, each company's market cap should equal the present value of the future cash flow it is expected to produce.)

Apple generates dramatically more operating cash flow than Amazon.com, even after the latter's rapid growth in recent years. It invests a relatively small proportion of this cash back into the business, frequently converting 80% or more of its cash from operations into free cash flow. In recent years, it has returned virtually all of this free cash flow to shareholders through dividends and share buybacks.

By contrast, Amazon.com has never paid a dividend and hasn't repurchased any shares in recent years. Indeed, Amazon reinvests virtually all of its operating cash flow into the business, after adjusting for the impact of its generous stock compensation and its heavy usage of capital leases.

AAPL Free Cash Flow (TTM) Chart

Apple vs. Amazon cash flow and cash usage. Data by YCharts.

So what does this all mean? From an investment perspective, Apple's market cap doesn't need to rise much to provide a good return for investors. Since Apple regularly doles out cash equal to 7% or more of its market cap in the form of buybacks and dividends, investors would get decent returns even if its market cap remained steady.

By contrast, Amazon hasn't been returning any cash to shareholders. Based on the company's aggressive reinvestment plans, it could still be many years away from doing so. Thus, for investors to earn good returns, Amazon's market cap must continue to rise at a rapid rate.

Amazon's market cap would reach $1 trillion in just a decade, if it grows 10% annually between now and then. If Apple delivers a similar 10% annualized total return but continues to distribute nearly all of its free cash flow to shareholders, its market cap might only inch up to $800 billion.

Of course, it's anyone's guess how Apple and Amazon will actually fare in the coming years. But with Amazon reinvesting nearly all of its earnings into the business while Apple returns the vast majority to shareholders, Mr. Market is betting that Amazon will surpass Apple's market cap within less than 10 years.