You wouldn't know it from all the attention on the "Magnificent Seven" stocks, but Walmart (WMT -1.35%) has quietly trounced the market in recent years.

Over the last three years, the retail giant is up 140%, compared to just a 52% return for the S&P 500. It has easily outperformed archrival Amazon as well, which is up 92% during that period.

Walmart has made smart investments in e-commerce and omnichannel fulfillment, including adding grocery pickup stations. It has also begun unlocking the potential of advertising on its e-commerce marketplace and beyond.

The company has executed well within its stores, managing inventory effectively and keeping prices low, which has helped it grab market share from competitors like Dollar General. It has also outperformed the retail sector during the recent inflationary period, as most of its sales come from consumer staples like groceries, for which it's easier to pass along price increases.

Walmart addresses tariffs

However, Walmart, which receives nearly one out of every 10 non-automotive retail dollars in the U.S., is back in the spotlight after it addressed the effect of tariffs.

Speaking on the recent earnings call, CEO Doug McMillon said: "We will do our best to keep our prices as low as possible. But given the magnitude of the tariffs, even at the reduced levels announced this week, we aren't able to absorb all the pressure given the reality of narrow retail margins."

He also noted that the cost pressure from tariffs began in late April and has accelerated through May.

Walmart's announcement caught the ire of President Donald Trump, who said in a Truth Social post that the company "should STOP trying to blame tariffs as the reason for raising prices," and said Walmart should "EAT THE TARIFFS."

A Walmart sign lit up at night.

Image source: Walmart.

Walmart's hidden advantage in a trade war

Like any retailer, Walmart would prefer not to have to deal with tariffs. But the company is in a better position than its peers to pass along higher costs, and to perform well in an inflationary or recessionary environment.

First, Walmart makes most of its revenue from food. The company said, "We won't let tariff-related cost pressure on some general merchandise items put pressure on food prices," though it noted cost pressure on some imported items like bananas, avocados, and coffee.

Thanks to its economies of scale and ability to move around supply chains, Walmart will likely do a better job of managing the additional costs around tariffs than smaller retailers. Its strength in food also gives it an advantage, as customers will continue to visit its stores for groceries.

Since it can bring in customers with groceries, driving customer frequency, it can more easily sell general merchandise products like electronics, toys, or clothes than a competing retailer that doesn't offer groceries, like Best Buy or Kohl's.

Walmart has significant China exposure -- about 60% of the manufactured products it sells -- but its reputation for "everyday low prices," economies of scale, and strength in groceries make it more resilient in a trade war or a down economy than most of its retail peers.

Is Walmart a buy?

While investors sent the stock briefly lower after Trump called out the retailer, its first-quarter earnings report showed that the company continues to operate like a well-oiled machine.

Same-store sales at U.S. stores rose 4.5% in Q1, and the company maintained its guidance for the year despite pressure from tariffs.

Plus, gross margin ticked up at U.S. stores, operating profits rose, and e-commerce and advertising sales were up 22% and 50%, respectively. This shows that Walmart is executing in its key growth categories.

The stock's valuation could put the brakes on growth in the share price, but the business looks as strong as ever. Even with the disruption from tariffs, the company's competitive advantages look poised to get stronger. The retail giant continues to look like a buy for long-term investors.