It looks like inflation is heating up again. That could mean higher interest rates for longer, thus tightening financial conditions that could eventually tip the economy into recession.

In such an environment, Walmart (WMT 0.02%) is a no-brainer for defensive investors. It is a consumer stronghold that does well when people want to tighten their budgets. But investors have gotten a head start on scooping up shares. The stock has soared over the past 12 months, appreciating 20%.

It's fair to wonder whether there's still opportunity here, or if Walmart's a trap waiting to ensnare unsuspecting investors. Here is what you need to know.

Walmart is a consumer bedrock

Walmart's size and scale are impressive. The company does a whopping $648 billion in annual sales and attracts business from North America and worldwide. The mega-retailer is the go-to for the American consumer. Approximately 90% of Americans live within 10 miles of a Walmart store, and Walmart is responsible for 25% of all grocery spending in the United States.

This size creates a competitive advantage that's tough to match. Walmart's massive scale gives it unique bargaining power with suppliers, enabling it to sell at famously low prices that your neighborhood mom-and-pop shops can't touch.

Perhaps you've seen it or done it yourself. Inflation has raised the cost of living, and more people are going to Walmart to save money. Walmart's comparable sales were up 4% in Q4, driven by grocery, health, and consumer products. General merchandise was soft. What does that tell you? People are shifting their spending from wants to needs.

Walmart can play that game well. Management believes comparable sales will grow 3% to 4% this year. That doesn't seem like much, but remember that Walmart is lifting a massive revenue number.

The price paid matters a lot

As impressive as it is that Walmart can continually grow despite its enormous size, that heft is also a problem. Walmart is too big to get careless with the price you pay for the stock. In other words, Walmart has become a slow and steady grower and can't quickly outgrow a high valuation. Overpaying for shares could mean subpar investment returns.

Today, Walmart is at the high end of its 52-week range. That makes sense. As discussed above, Walmart will do well in an environment like this one, where people are trying to save money. But shares trade at almost 26 times this year's estimated earnings at this price.

WMT PE Ratio (Forward) Chart

WMT PE Ratio (Forward) data by YCharts

How can you tell whether that's expensive? I like to use the price/earnings-to-growth (PEG) ratio, which compares a stock's valuation to its expected earnings growth. Analysts believe Walmart will grow earnings by an average of 6% to 7% annually over the long term. The resulting PEG ratio is 3.8, an egregiously high number. For reference, I like to buy stocks when the PEG ratio is 1.5 or less. That means that Walmart could drop 50% from here and still have an argument that the stock is expensive.

Is it too late to buy Walmart?

A good business isn't necessarily a good investment. This perfectly describes the state of Walmart's stock today. Shares are too expensive to give investors a favorable shot at satisfactory investment returns over the coming years. That doesn't mean those holding the stock must run out and sell their shares, though I would understand -- its shares are that expensive now.

Prospective investors are better off looking at other investments. A stock like Amazon is more attractive today, even at its all-time highs.