Even the best companies can sometimes experience a sluggish phase, where it might seem that a top company is past its peak growth years. It's a good sign that you have found a stock that can generate returns for decades when a battered company can adapt to the marketplace and resume its previous growth trajectory.

Walmart (NYSE:WMT) and Microsoft (NASDAQ:MSFT) are two best-of-breed companies that have made dramatic comebacks in recent years. E-commerce growth has fueled a 59% rise in Walmart's share price in the last three years. Meanwhile, Microsoft stock has climbed 140% since 2016 from robust growth in cloud services. Here's why you can buy these stocks and hold them for decades to come.

A plant growing out of a pile of coins with a city skyline in the background.

Image source: Getty Images.

The new e-commerce giant

Walmart is not the stodgy, big-box retailer you knew 10 years ago. CEO Doug McMillon has the company on a roll. During his tenure, Walmart has invested heavily in technology, especially in e-commerce, where sales increased 37% in the recent quarter. The company is coming off an impressive second-quarter earnings report that saw U.S. comp sales growth, on a two-year stacked basis, reach 7.3% -- the best growth rate in more than 10 years. 

E-commerce is now a $25 billion annual business for Walmart, which has investors optimistic that the once-stagnant retail giant is not going to roll over to the onslaught of Amazon.com. The groundwork for where Walmart is today was laid with the acquisition of Jet.com in 2016, along with other partnerships with tech companies across India, China, and Japan. 

Walmart is investing aggressively in technology, including the use of robots to manage inventory at stores. Additionally, there are significant upgrades underway in supply chain logistics to accommodate the wave of customers who are ordering online. These investments are not cheap. Walmart is spending $1.2 billion to upgrade one facility in China for omnichannel capabilities. And it plans to upgrade 10 distribution centers in China over the next 10 to 20 years -- doing the math suggests this is potentially a $10 billion to $20 billion commitment -- which shows that it has its eye on the long game.

Most importantly, Walmart is continuing to win in grocery delivery, which has been a crucial driver of e-commerce growth for the Arkansas-based retailer. It is on pace to have more than 3,100 stores offering free grocery pickup and 1,600 offering same-day delivery by the end of the year. The company is keeping its foot on the gas pedal with plans to expand grocery delivery with an unlimited and in-home service this year, which should keep its momentum going in this important area. 

Walmart is playing hardball now, and it has a vast footprint of stores around the world to conveniently serve customers who order groceries online. This is why I believe it's a stock worth holding for the long term. 

The new cloud services giant

Like Walmart, Microsoft has experienced something of a comeback in the last decade. It went through a phase several years ago where growth was getting more difficult to come by, stemming from a stagnant PC market. The company was behind the curve on mobile, where people wanted to be able to access their content across any device. Microsoft needed to change how it did business, and that took a new CEO in Satya Nadella, who had a background working in the cloud. 

Over the last five years, Microsoft has disengaged itself from dependency on the PC to be a platform-agnostic company. The shift in strategy to the software-as-a-service market has sent the stock soaring 191% since 2014. It is coming off an impressive year of growth, with revenue and adjusted earnings per share up 14% and 22%, respectively. The strong results were fueled by double-digit growth in cloud services, including Office 365 commercial, and growth in the Intelligent Cloud segment. What these results show is that Microsoft's competitive advantage, derived from the familiarity that 800 million Windows users have with its software, is still as strong as ever. 

But Microsoft's future growth will likely come from its booming cloud business. It's seeing exceptional growth in the infrastructure-as-a-service cloud market, where the company is positioned solidly in second place and is gaining ground on the leader. In the last quarter, the Intelligent Cloud segment increased revenue by 19% year over year, driven by 64% growth in the Azure enterprise cloud business. 

Azure is still way behind Amazon Web Services in market share, but Microsoft may eventually eclipse the e-commerce giant. Both Amazon and Microsoft offer great technology for cloud customers, but Microsoft has one advantage that could see Azure win out in the long run: Unlike Amazon -- a do-it-all online retailer -- the company isn't threatening to compete with its cloud customers. This is the main reason Azure continues to win big customers like Walmart and other large corporations. 

Microsoft is deeply entrenched with consumers and businesses alike. It also has its foot in the door in the growing $152 billion video game industry with its Xbox, where Xbox Game Pass has 65 million subscribers and counting. The software giant will certainly have a big role to play over the next decade as cloud gaming services take root.

The stock is not cheap, but I also don't think its current forward P/E of 23 times next year's earnings estimate is too high, considering the company's enormously wide competitive moat and its current momentum. Microsoft has the qualities any investor would want as a core holding for a nest egg.