If you weren't an investor, you might think neighborhood convenience stores like these two -- Walgreens Boots Alliance (NASDAQ:WBA) and Rite Aid (NYSE:RAD) -- made their dough selling snacks, soda, and household items. But the truth is that the pharmacies within the stores are their golden goose.

While neither company's stock has come close to beating the broader market over the past five years, Walgreens' shareholders have fared much better, earning 19% returns versus an enormous 88% loss for Rite Aid over the same time frame. But the stock market cares more about the future than anything else. While it's impossible to know for certain which company is a better buy at today's prices, let's evaluate these two on three different facets to see which comes out ahead.

Happy senior male customer buying medications at drugstore

Image source: Getty Images

Financial fortitude

When I talk about financial fortitude, what I really want to know is: How would the company be affected by an immediate and fairly long-lasting financial downturn? Companies with lots of debt and little cash flow are apt to fold, but those in the opposite circumstances -- lots of cash, little debt, and strong cash flows -- can actually grow stronger, by acquiring rivals and grabbing market share while weaker players are forced out.

Keeping in mind that Walgreens is valued about 80 times higher than Rite Aid, here's how the balance sheets stack up:

Company Cash Debt Free Cash Flow
Walgreens $7.6 billion $11.6 billion $6.3 billion
Rite Aid $417 million $3.4 billion ($278 million)

Data source: Yahoo! Finance. Cash includes long and short-term investments. Free cash flow presented on trailing twelve-month basis.

This is really no contest. While Walgreens technically has a bigger debt load -- with a net cash position of negative $4 billion versus Rite Aid's negative $3 billion -- we need to look at the bigger picture. Walgreens has massive free cash flows coming in the front door, making debt payments an easy task to handle. Rite Aid does not, which helps explain why the company is in such dire straits.

Winner = Walgreens

Valuation

Next we have the murky science of valuation. No single variable will tell you which stock is "cheaper" or more "expensive". But by consulting a number of different variables, we can get a good enough picture to make a call between two potential investments.

Company P/E P/FCF PEG Ratio Dividend FCF Payout 
Walgreens 13 11 1.1 2.4% 28%
Rite Aid N/A N/A 10.1 N/A N/A

Data source: Yahoo! Finance, E*Trade. P/E presented using non-GAAP earnings when applicable. 

It's no contest. Rite Aid is unprofitable, has negative free cash flow, and doesn't offer investors a dividend. Walgreens trades for just 13 times earnings and 11 time free cash flow. That P/E puts the company at a 35% discount to the S&P 500! 

Walgreens offers a moderate dividend that only eats up 28% of the company's free cash flow, meaning it's safe and has plenty of room to grow over time.

Winner = Walgreens

Check out the latest Walgreens earnings call transcript.

Sustainable competitive advantages

Finally the most important thing to investigate: a company's sustainable competitive advantages, or its moat. When it comes to convenience/drug stores, the primary moat a company can have is high switching costs.

It's certainly not impossible for a person to decide to change where they get their prescriptions filled. But once we start getting medication at one pharmacy location, we tend to stick with it until we change where we live. When you switch, there's new paperwork to fill out, insurance information to provide and you must change your primary pharmacy on file with all your medical providers. Not to mention that if people like their pharmacist and find the experience convenient, they're unlikely to find cause to switch.

One crucial thing to understand about this comparison is that Rite Aid has been furiously trying to sell or merge with another company for over two years. Momentarily, it appeared Walgreens might buy Rite Aid, but that deal fell through because of antitrust concerns. Instead, Walgreens purchased more than 1,900 Rite Aid locations for $4.4 billion in 2017. Last year it looked like Rite Aid finally had a solution in the form of a merger with privately held Albertsons, which fell apart too.

Both companies have relationships with drug manufacturers that bolster their positions, and both face pressure from CVS Health, which is diversifying by entering the health insurance market via the Aetna acquisition.

In the end, the real difference between the two is the fact that Walgreens has over 9,400 locations in total, versus just over 2,500 for Rite Aid. But I wouldn't consider either's moat to be terribly large: Amazon has already signaled its interest in entering the prescription market, and the company could quickly erode any advantages the brick-and-mortar locations have. Who doesn't love getting things delivered to their doorstep?

Between the two, Walgreens wins, but I'm not optimistic about either's moat.

Winner = Walgreens

And my winner is...

So there you have it: Walgreens has a slightly wider moat, and a much better balance sheet. It's the clear favorite.

But I don't own Walgreens, nor have I made an out-perform call on my CAPS profile for the stock, because I think the possibility for Amazon to disrupt the prescription industry is too great, and I don't think my money would be well-invested in any of these players over the long-term.