Forget the difference in market caps. CVS Health Corporation (NYSE:CVS) and Wal-Mart Stores, Inc. (NYSE:WMT) are fierce rivals. The fact that Wal-Mart is valued nearly four times greater than CVS doesn't change that fact one bit.

You can also forget that Wal-Mart stock has racked up solid gains so far this year, while CVS Health stock hasn't. When it comes to investing, the past isn't nearly as important as the future. Which of these two stocks is the better choice for investors over the long run? Here's how CVS Health and Wal-Mart compare.

CVS pharmacy sign

Image source: The Motley Fool.

The case for CVS Health

These aren't the greatest times for CVS Health. The pharmacy services company had its lunch handed to it by rival Walgreens Boots Alliance (NASDAQ:WBA) in the second half of 2016. Walgreens took two big pharmacy contracts away from CVS Health.

In a way, though, these setbacks make CVS Health a more intriguing investment option. As a direct result of the Walgreens wins, CVS stock sank like a brick. Its share price still hasn't rebounded much. That's not good, but it makes CVS Health's valuation look more attractive than it's been in a while. Shares now trade at only 12 times expected earnings. 

While losing business caused CVS Health to cut its revenue outlook, the company's long-term prospects seem to be solid. CVS is probably best-known as the nation's largest pharmacy retail chain. It also owns Omnicare, the largest provider of pharmacy services to long-term care organizations. These businesses should grow over the long run as more Americans age and require more prescription drugs.

CVS Health also ranks as the second-largest pharmacy benefits manager (PBM). The company's PBM business segment help control prescription drug costs for employers, insurers, and government agencies. Demographic trends in the U.S. along with increasing prescription drug spending should help drive demand for CVS Health's PBM services.

Wall Street analysts project that CVS Health should be able to grow annual earnings by nearly 8% over the next five years. The company itself is shooting for average earnings-per-share growth of around 10% over the long term, boosted by share buybacks.

On top of this growth, CVS Health offers a solid dividend with a current yield of 2.57%. The company has expressed its commitment to dividend hikes in the future. With a low payout ratio of 37%, CVS Health should be able to deliver on its promise. 

Walmart store

Image source: The Motley Fool.

The case for Wal-Mart

While CVS Health stock has stumbled, Wal-Mart stock has marched steadily ahead. And it's done so despite relatively slow revenue growth and earnings declines over the last three years.

There were two key reasons why earnings have dropped. First, Wal-Mart restructured the wage structure for its associates. Second, the company has invested in digital retail and information technology. However, both of these factors should help Wal-Mart compete more effectively in the future.

Wall Street expects Wal-Mart will indeed return to earnings growth in the years ahead, with projections of average annual earnings increases of nearly 6%. That investment in technology already appears to be paying off to some extent. In the first quarter of this year, Wal-Mart reported a 69% increase in e-commerce sales worldwide and 63% growth in the U.S.

Wal-Mart's latest acquisition shows how seriously the company is taking e-commerce. In June, the company announced its plans to buy upscale men's clothing retailer Bonobos for $310 million. This move will give Wal-Mart another foot into a premium market that's much different from its core low-price market. 

If you're looking for perhaps the best reason to buy Wal-Mart stock, check out its cash flow. Last year, the company generated free cash flow of $20.9 billion -- up 31% from the previous year. That strong cash flow not only allows Wal-Mart to fund acquisitions and reinvest in improving operations, but it also enables the company to fund a solid dividend. Wal-Mart's dividend yield currently stands at 2.67%, with a low payout ratio of less than 46%.

Better buy

As big as CVS Health is, compared to Wal-Mart, the matchup looks like David versus Goliath. We know who won that battle. 

The two companies stack up fairly evenly on their dividends. However, CVS Health should enjoy higher growth in the next few years. It's also more attractively valued right now. I think CVS Health is the better pick for investors.

There's one thing to keep in mind with CVS and Wal-Mart, though: Both companies have to watch out for competition from a certain e-commerce company named after a South American river. While Wal-Mart faces the greater threat right now, CVS Health won't be immune from competition should Amazon.com enter the pharmacy market.

Keith Speights has no position in any stocks mentioned. The Motley Fool owns shares of and recommends AMZN. The Motley Fool recommends CVS Health. The Motley Fool has a disclosure policy.