A BOA constrictor swallowing capitalism. A cyclone dragging the economy into its vortex. If you look back at how people described Walmart a decade ago, it is eerily similar to how Amazon is viewed now. The supermarket chain has “a scale of economic power we haven’t encountered before”, warned “The Walmart Effect”, a best selling book in 2006. But capitalism never stands still. The world’s largest company by sales is now the perceived underdog in an escalating grocery war with Amazon to fill 320m American bellies. The struggle will probably end in a messy stalemate. That will mean mediocre returns for investors—and happy days for consumers.

Just when Walmart’s aura was at its most intimidating, in 2006, stagnation beckoned. Its reputation for bullying its suppliers and staff became toxic. Over the next decade it hit saturation point. About 95% of Americans shop at Walmart at least once a year. It has three square feet of shop space for every adult in the country and has sunk $83bn into a fixed-asset base that is the fourth-largest owned by any American firm. Investors have worried for years that this empire of aisles and tills run from Bentonville, Arkansas, would become obsolete—what did Walmart’s executives, schooled in the arts of beating up baked-beans suppliers, know about the slick world of e-commerce being dreamed up in Silicon Valley and Seattle?

More than you might think. This year Walmart’s shares have risen by 40% on hopes that it has more than a fighting chance. It is clear that selling groceries online is very different from selling books. In food, penetration of e-commerce is low, at 2%, compared with 9% for all retail. Food is perishable. People will not stuff it in their mouths unless they trust its provenance. They also want flexibility—to buy food in a store, to order online and pick it up themselves, or to have it delivered to their homes. So some physical infrastructure is helpful. “I wouldn’t want another set of assets,” Doug McMillon, Walmart’s boss, told the Economic Club of New York in November.

He has run Walmart’s businesses in Europe and Asia, where e-commerce for groceries is more developed. In 2016 Walmart spent $3bn buying Jet.com, an e-commerce firm whose boss, Marc Lore, now runs all Walmart’s online operations. Walmart has launched internet-based services such as “Easy reorder” and “Pickup discount” and formed a partnership with Latch, which lets its users open and lock their front doors remotely. On September 29th it acquired Parcel, a logistics startup. On December 6th it changed its legal name from Walmart Stores, to just Walmart.

There are three reasons to be optimistic. First, Walmart’s performance has improved. In the most recent quarter, same-store sales rose by 2.7% year on year, and store traffic by 1.5%. Food sales increased at their fastest pace in six years. Sales from e-commerce represent only about 2% of the total but are rising at an annual rate of 50% (customers who shop online and in stores typically spend twice as much as those who only go to stores). Walmart has the second-most-downloaded retail app, after Amazon’s.

Second, Amazon’s behaviour is a backhanded compliment. In June it spent $14bn on Whole Foods, a mid-sized grocery chain, The deal brings Amazon more physical locations to sell, sort and dispatch goods. It also gives it trusted private-label brands, of the kind Walmart already has. If you type “spinach” into Amazon.com, bags of Whole Foods branded greenery appear.

Lastly, the example of China points to a fusion of the online and physical worlds. In some ways the country is more advanced than America; e-commerce comprises 9% of grocery sales, according to Alliance Bernstein, a research firm. On November 20th Alibaba, an e-commerce giant, bought 36% of Sun Art Retail, a hypermarket retailer. Four of the six biggest Chinese supermarket chains have partnerships with e-commerce platforms. (Walmart, which has 424 stores in China, has teamed up with JD.com.)

The duel between Walmart and Amazon could go in two directions. It might escalate into a war across America, for both companies hate losing. Or each firm might conquer different geographical areas and demographic groups. Amazon could seize well-to-do cities, where population density is high and home delivery is more efficient. Walmart could continue to rule suburbia.

Either way, margins will probably be squeezed as Amazon throws money at the fight with its customary abandon. Mr McMillon knows this. “One of the challenges at Walmart is that we don’t have free money—we are expected to make a profit,” he says. The danger is that it overestimates how much physical presence it needs. If it went back to its position in 2006, it could cut its domestic asset base by 34% and still have 90% of Americans within 15 miles of a store. For every dollar of sales, it has twice as many square feet of sales and distribution space as Amazon’s retail operation (including Whole Foods). If Mr McMillon is brave he will sell stores and return capital to investors. Walmart needs to make its balance-sheet leaner.

What’s in store for Amazon

Walmart is probably the most formidable adversary Amazon has ever faced. Disrupting the music, book and media industries, each known for their Corinthian spirit and long lunches, was child’s play compared with taking on Walmart, with its fanatical commitment to low prices.

Walmart’s history is also a warning. If you examine the two companies’ financials, Amazon today looks almost identical to Walmart in 1999. It has annual sales of $160bn or so, low margins, fast growth, a ballooning asset base and massive capital investment. The firm’s managers are on a high and investors have dizzying expectations for the future. But for the ten years after 1999, Walmart’s share price was as flat as a pancake, because all the good news was already baked in. The actual business of world domination turned out to be a long, hard slog.