Despite the onslaught of nimble e-commerce start-ups over the last 20 years, Costco Wholesale (NASDAQ:COST) is still outperforming the broader market. In the previous five years, shares of Costco have increased by 86% versus the S&P 500's return of 42%. 

On the other side, it seems Amazon.com (NASDAQ:AMZN) can do no wrong. Even when it makes a mistake (like the Fire Phone), Amazon keeps growing. Over the last five years, the stock has shot up 306%, which puts it up nearly 32,000% for the last 20 years.

Despite both companies' strong history of returns, past performance does not assure future results. With that in mind, I'll compare both companies' financial fortitude, valuation, and competitive advantages to determine which stock is the better buy for investors.

A shopping basket connected to a computer mouse.

Image source: Getty Images.

Financial fortitude

It's always beneficial to invest in a company that has a rock-solid balance sheet. The company that generates more free cash flow and has more cash sitting in the bank can weather downturns in the economy and expand its competitive moat while competitors flounder.

Additionally, cash can be used to reward shareholders in the form of share repurchases and dividends.

Here's a look at how Costco and Amazon stack up on key financial metrics:

Metric Costco Wholesale Amazon.com
Cash $7.953 billion $29.765 billion
Debt $6.48 billion $24.684 billion
Free cash flow (TTM) $3.066 billion $13.36 billion
Revenue (TTM) $144.84 billion $220.96 billion
Free cash flow as a percentage of revenue 2.1% 6%

Data source: The companies and Y-Charts. TTM = trailing 12 months.

Amazon is a larger business, with $221 billion in trailing-12-month revenue versus Costco's $145 billion. As a result, we should expect Amazon to have more cash on the balance sheet. Amazon generates significantly more free cash flow as a percentage of revenue than Costco, which tilts the advantage to the online retailer.

Winner: Amazon.

Valuation

Here's how Costco and Amazon compare on a range of popular valuation metrics:

Metric Costco Wholesale Amazon.com
Trailing P/E 28.7 94.6
Forward P/E 27.4 62.2
P/Free cash flow 30.5 63.0
P/Sales 0.65 3.8
PEG ratio 2.46 2.0
Dividend yield 1.05% N/A (doesn't pay a dividend)
Dividend payout as a percentage of free cash flow 38.81% N/A

P/E = price to earnings. PEG = price/earnings to growth. Data source: Y-Charts and Yahoo! Finance.

Neither stock is cheap, but Costco is more conservatively valued. Amazon does have a lower PEG (price-to-earnings-growth) ratio, but not by much. We are long in the tooth of a bull market, and any slowdown in Amazon's growth would likely cause its high P/E to contract, which would hurt returns in the short run.

Overall, Costco's significantly lower P/E ratio and dividend yield make the stock a better value. Amazon doesn't pay a dividend.

Winner: Costco.

Competitive advantage

Both companies are focused on keeping costs down to sell merchandise at competitive prices. Costco is cheaper on most items, but that hasn't prevented Amazon from growing more than the warehouse store over the years. Despite Amazon's ascent, Costco still looks poised for long-term growth, given it has an 88% membership renewal rate worldwide, and the company has seen higher sign-up rates from millennials, which is an encouraging sign for future growth

However, Amazon has the advantage here because the company has grown much faster and is chewing up market share of brick-and-mortar retailers. For example, 20 years ago, Costco generated $24 billion in revenue when Amazon was a tiny fraction of that size. In 2016, Amazon surpassed Costco in total revenue. Amazon has grown the top line 183% over the last five years, while Costco increased revenue just 34%. 

Of course, part of Amazon's faster rate of growth is due to its booming cloud business (Amazon Web Services). However, even if we isolate Amazon's core retail business in North America, revenue growth is still robust, up 35% in the third quarter. Amazon is set to overtake Walmart as the No. 1 apparel retailer in the U.S., according to research from Wells Fargo

The online, do-it-all juggernaut is leaving Costco in the dust. The combination of Amazon Prime, Echo smart-home devices, and Amazon's unrivaled customer service is winning over loyal shoppers in droves. Amazon could also gain significant share in grocery -- Costco's home turf -- through its acquisition of Whole Foods. Costco management admitted during the last earnings conference call that it saw some competitive pressure in groceries from larger rivals. 

Winner: Amazon.

Amazon is the better buy

And there you have it. Costco wins on valuation, and it offers a dividend yield, which income investors should favor. However, Amazon stock will likely outperform Costco shares over the long term, even with Amazon's higher valuation.

There are two main reasons that Amazon's growth is far from over. First, e-commerce still makes up less than 10% of domestic retail sales. Second, Amazon still has a tremendous opportunity to grow its cloud business. One analyst is on record stating that Amazon Web Services could be worth nearly half of Amazon's current market capitalization in just a few years.

Amazon has an $801 billion market cap, but it's still growing like a start-up business. For all of these reasons, investors are better off paying up for Amazon.

John Mackey, CEO of Whole Foods Market, an Amazon subsidiary, is a member of The Motley Fool's board of directors. John Ballard owns shares of Amazon. The Motley Fool owns shares of and recommends Amazon. The Motley Fool recommends Costco Wholesale. The Motley Fool has a disclosure policy.