For years, Shopify (SHOP 0.90%) and Walmart (WMT -0.05%) were very different companies. Both were involved in commerce, but Shopify was completely focused on e-commerce, while Walmart's business was heavily dominated by brick-and-mortar sales.

Today, both companies are competing more directly than ever. Shopify still retains its e-commerce focus, but Walmart has invested hundreds of millions of dollars to bolster its digital sales channels. So far, Walmart's bet appears to be paying off.

With both stocks capable of strong growth in 2024 and beyond, which is a better buy right now?

You should be betting on digital

The future is digital. It took Walmart longer than expected to accept this reality, but the company is now fully onboard.

From 2000 to 2020, Walmart saw its quarterly revenue growth rates slip from the teens to nearly 0%. In some quarters, the company's revenue actually shrank. This was partially due to a saturated market. Walmart had already grown into one of the largest retailers in the U.S., with stores in every state. In 2018, the company actually began closing more stores than it was opening. Today, the company operates 10,623 stores -- more than 1,000 fewer stores than in 2018.

Walmart's biggest issue, however, wasn't an inability to grow its physical store count. Rather, the company struggled to compete with online retailers like Amazon, which were taking away a large chunk of its sales.

After decades of underinvestment, Walmart finally got serious in recent years, investing hundreds of millions of dollars into new automated shipping and fulfillment centers to compete with Amazon's speedy delivery capabilities. Walmart also rolled out new programs like Walmart+, which was modeled after Amazon's Prime service.

Today, Walmart's revenue growth rates are back near long-term historical averages, with e-commerce growth a big part of this revival. Net sales were up 3.4% last quarter, fueled by a 17% jump in e-commerce sales. Walmart's market saturation has even turned into a growth engine. Roughly 90% of the U.S. population now lives within 10 miles of a Walmart. That allows Walmart+ subscribers to take advantage of both online and physical location benefits in a way that Amazon can't match.

WMT Revenue (Quarterly YoY Growth) Chart

WMT Revenue (Quarterly YoY Growth) data by YCharts

Shopify, of course, didn't need to copy Walmart's evolution, given that it was a digital-first company from the start. Shopify's e-commerce platform is essentially Amazon on steroids. If merchants want to sell through Amazon, they have to sell on Amazon's terms. If they want to sell through their own channels, it historically took a lot of time and money to set up and run an online storefront. Then Shopify showed up. Its services mean that with a few clicks, anyone can begin selling online with a full host of features and functionality, including easy-to-use web design, inventory tracking, payment processing, and much more.

While Walmart is growing sales by 5% to 6% annually, Shopify is growing sales by more than 20% annually. That's the benefit of being a smaller company focused on a higher growth segment of the commerce market.

However you slice it, whether it's Walmart's recent push into e-commerce or Shopify's initial e-commerce focus, the future is digital. Right now, both companies are positioned to benefit.

SHOP Revenue (Quarterly YoY Growth) Chart

SHOP Revenue (Quarterly YoY Growth) data by YCharts

Which stock is a better buy right now?

Both Shopify and Walmart are betting on digital, and both are seeing success with their strategies. But which stock is a buy right now? Let's take a look at each company's current valuation.

As a smaller, tech-focused company, Shopify's stock trades at a steep premium. Shares currently trade at 14 times sales. Walmart, for comparison, trades at just 0.75 times sales. But there's another way to look at each company's valuation. For instance, Shopify's stock trades at a free-cash-flow yield of just 0.9%, while Walmart's shares have a free-cash-flow yield of 3.1%. On a profits basis, Shopify's stock trades at 870 times earnings, while Walmart trades at just 31 times earnings.

All of this paints a clear picture: A bet on Walmart today is a bet on a slower-growth business with high levels of existing cash flow and profits, while Shopify is a high-growth business that is sacrificing near-term cash flow and profitability to keep growth rates elevated.

Which stock is right for you? That depends on your time frame and risk tolerance.

Shopify has a highly efficient business model with strong competitive advantages. Some calculations show it commands a 28% share of the U.S. e-commerce platform market. Its technology and market share leads should provide a multidecade growth runway, which could eventually make its premium valuation look like a steal.

Walmart, meanwhile, is experiencing what could become a multidecade growth runway focused on e-commerce. However, its core business -- physical sales -- is already huge and stagnating. That'll limit growth rates, but the company's reasonable valuation more than reflects that reality.

If you're willing to take a little extra risk and commit to a long holding period, Shopify looks like a great choice. Shopify's stock certainly has the most long-term upside. Walmart, however, is still a great pick for more conservative investors, especially if its e-commerce success continues into future years.