Walmart ended a week of speculation Monday when it announced it would buy Jet, an e-commerce startup that specializes in bulk orders, for $3.3 billion. It's the most expensive purchase ever of an online retailer. It's also really smart.

On the surface it might seem like an odd match. Jet is barely a year old — it was founded by CEO Marc Lore, in July of 2015, after Lore had already sold another online commerce startup to Amazon for over $500 million. But despite considerable fanfare around its opening, Jet has yet to prove itself: Eighty-one percent of U.S. consumers still don't know the company exists, and Jet has been burning through cash to build out its staff, marketing, and customer base. It had to drop its $49 membership fee last October, and it doesn't anticipate becoming profitable until 2020.

"If you looked at Jet from a fundamentals perspective, the company wouldn’t be worth what they’re paying for it," Anand Sanwal, the chief executive of venture capital research firm CB Insights, told The New York Times.

But dig a little below the surface, and Jet and Walmart seem to complement each another well.

Let's start with Walmart.

While it remains the world's largest company by revenue — last fiscal year, it pulled in a whopping $482 billion in sales — it's still primarily a brick-and-mortar company and struggles with online retail. That's not to say it doesn't have any online presence. In fact, it's the second biggest online retailer in the U.S. The problem is that the biggest, Amazon, is so far ahead of it.

Raking in over $79 billion in revenue last year, that e-commerce colossus alone accounted for nearly two-thirds of the growth in online sales in 2015. By contrast, Walmart's online sales only reached just over $13 billion in 2015, or 2.5 percent of its business.

And up until now, Walmart has done very little to change this dynamic. Yes, it's been keeping pace online with Amazon, growing at 12 percent to Amazon's 13 percent over the course of last year. But Walmart's online business starts from such a massively smaller baseline that it needs to do something extraordinary to catch up.

More troublingly, Walmart has stumbled recently instead: Amazon grew 31 percent in the most recent quarter, while Walmart's online business grew just 7 percent — less than half the internet retail average. And Walmart is losing out to more traditional competitors, too: Staples almost equaled Walmart's online sales last year, and Target's online business — while much smaller — has recently grown a remarkable 30 percent.

Bourree Lam noted at The Atlantic that the overall world of retail may finally be reaching the tipping point where no one can ignore the online side of the business anymore: "In the past decade, Walmart has largely focused on its sales in its physical locations. But earlier this year, the company announced the closure of over 150 U.S. stores." Observers also argue that Walmart's online site just isn't as user-friendly as Amazon's. So plenty of people now see the rise of online sales as a threat to Walmart's business.

The purchase of Jet and the melding of its institutional expertise with Walmart is a way to deal with all this. In fact, Lore himself will take over running Walmart's online activity once the deal goes through.

Then there's Jet's side of things.

While it's currently in the red, Jet is hardly the first tech startup to spend its early years focusing on growth and sales over profits, trusting the latter will come in time. Its sales in February 2016 were 50 percent higher than in September 2015. Walmart said Jet has added 400,000 shoppers monthly, and is on track to reach $1 billion in sales annually — and all of that after just one year of operation. The merger should also speed along the process by which Jet gets its finances into the black.

But Jet's real value lies in the algorithm it developed, which has allowed it to price competitively against even Amazon. Traditionally, brick-and-mortar retailers that rely on bulk in their business model let you buy a whole lot of each individual item — five pounds of mayonnaise or whatnot. The idea behind Jet, rather, is to allow shoppers to buy lots of smaller items — a normal amount of mayonnaise, of toilet paper, of cleaning supplies, etc. — all in one bulk purchase. And then, the algorithm accounts for what a customer wants to buy, where they live, and which retailers have shipping capacity nearby, and uses that information to game out the cheapest way to get all those items to them. As customers add items to an order, all the items get cheaper. And as a result, Jet's been able to offer prices that are 10 to 15 percent lower than its competitors.

The one wrinkle is this business model relies heavily on the suppliers — their locations, their physical infrastructure, and the ability to coordinate all those pieces. That's where Walmart comes in: It boasts an enormous physical infrastructure of stores and distribution centers, all of which can now be placed at the beck and call of Jet's algorithm.

Mergers between established powerhouses and young guns are always rife with uncertainty. But at least on paper, this one looks like a perfect match.