Markets are very optimistic about Tesla’s future — so optimistic that it would be impossible to explain if Tesla were a conventional car company.

On paper, Tesla shouldn’t be worth anywhere near as much as rivals like Ford and GM. Ford sold 6.6 million cars in 2016. GM sold 10 million. Tesla sold a paltry 76,000. Ford and GM both turned healthy profits in 2016. Tesla lost money.

Yet earlier this month Tesla’s market value surged past Ford and then briefly eclipsed GM as America’s most valuable carmaker.

One big reason for this is that Tesla has made a risky bet on batteries that could be on the verge of a huge payoff. Because Tesla cars are purely electric, a single car needs roughly 5,000 times as much battery capacity as a typical smartphone. So making electric cars a mainstream technology will require producing batteries on a scale that dwarfs today’s production for smartphones and other portable gadgets.

A major test of this will come in Tesla’s release of the Model 3 later this year. These cars will be powered by batteries from the Gigafactory, a huge factory Tesla has constructed in the Nevada desert. If the Model 3 is a hit, experts say, the Gigafactory will ensure Tesla has plenty of batteries to meet demand for this relatively affordable mass-market vehicle. Other car companies would have to scramble — not only to design a similar stylish vehicle, but also to find suppliers for yet more batteries.

Experts say this battery advantage won’t last forever — other battery makers might be able to catch up within a year or two. But having a year or two head start could make a big difference — not only cementing Tesla’s reputation as the leading electric car brand, but positioning Tesla to make further investments that could help it stay a step ahead of rivals down the road.

Why Tesla decided to make its bet on batteries

Anticipating this surge in demand, Tesla CEO Elon Musk made a big bet on the Gigafactory around 2013. The factory began producing batteries late last year. Tesla hopes that Gigafactory will not only give it an ample supply of batteries at a time when its rivals will be supply-constrained, it also hopes that economies of scale will allow it to produce batteries more cheaply than anyone else.

The Gigafactory began operation a few months before the expected launch of the Model 3 this summer. With a price around $35,000, Tesla is hoping to sell hundreds of thousands of the car per year. If the Model 3 is a hit, it could put other car companies in a bind. They’ll be scrambling to expand production of their own affordable electric cars to compete with the Model 3. But without Gigafactories of their own, there might not be enough batteries to go around.

“Musk has been the visionary,” said Steve LeVine, a journalist at Axios who has written a book about the battery industry. “He has been willing to take the plunge all the way along, from the very beginning.” In contrast, he told me in a phone interview, “Detroit has approached this race so cautiously.”

In the next couple of years we’ll find out if Musk’s big bet will pay off.

A chicken-and-egg problem

Tesla is investing a huge amount of money in the Gigafactory. Tesla originally promoted the factory as a $5 billion project, though it hasn’t said how much it actually spent so far. It’s a joint venture with Panasonic and supported by generous tax breaks from Nevada, where the factory is located.

Tesla Chief Technology Officer JB Straubel laid out the strategy behind the Gigafactory in a 2014 speech.

“It's pretty amazing how much growth there will be in energy storage,” he said. “We're doubling complete worldwide capacity for energy storage in a single factory. This is a growth phase where we're reinventing the whole supply chain and how we build this stuff. There are major opportunities for how we can reduce the cost of all the components going into that.”

Tesla believed that building such a big factory — with about as much battery capacity as the rest of the world put together at the time — would allow it to dramatically reduce costs.

“We're going far upstream in the cell manufacturing process,” Straubel said in 2014. “We're actually going all the way back to literally where the raw materials come from. Even those materials costs can be reduced if you drive the right volume and drive purchasing power into how you're buying them.”

No one outside Tesla (and its Gigafactory partner, Japanese battery company Panasonic) knows if Tesla has met its cost-reduction goals. But LeVine says that there’s solid research backing up Tesla’s assumptions.

“If you could produce 100,000 cars — and no one does,” LeVine says, there’s an “inflection point where costs drop. That's what the Gigafactory is all about: trying to get to 100,000 cars and beyond.”

Building such a massive battery factory was a huge risk — especially for a company as small as Tesla was at the time. The Gigafactory is designed to supply enough batteries for the 500,000 cars it hopes to produce per year in its Fremont, California, factory by 2018. But Tesla only sold 22,450 units of its flagship Model S sedan in 2013, the year Tesla started talking about the Gigafactory. If demand for 500,000 cars doesn’t materialize by 2018, Tesla will find itself with an extremely expensive battery factory it didn’t need. (Tesla has hedged its bets somewhat by also selling batteries for use in homes and by electric utilities)

But Musk believed there was no other way to quickly scale up Tesla’s business.

“I know all the suppliers, and I can tell you that they don’t like the idea of spending several billion dollars on a battery factory,” Musk told Ashlee Vance for his 2015 biography of Musk. “You’ve got a chicken-and-egg problem where the car companies are not going to commit to a giant volume because they’re not sure you can sell enough electric cars. So I know we can’t get enough lithium ion batteries unless we build this bloody factory, and I know no one else is building this thing.”

“The big car companies are so derivative,” Musk told Vance. “They want to see it work somewhere else before they will approve the project and move forward.”

Next year, we’ll find out if Musk’s big bet on batteries will pay off. Tesla is still nowhere close to producing 500,000 cars per year, but it has 400,000 preorders for its Model 3 and expects to begin production later this year at its Fremont factory. If customers like the Model 3 and Tesla figures out how to operate its Fremont factory at scale, then all those Gigafactory batteries will enable Tesla to leap far ahead of other automakers in the number of electric cars they produce.

On the other hand, if the Model 3 is poorly received — or production snafus in Fremont prevent Tesla from reaching full capacity — the result could be a disaster for Tesla’s bottom line, with much of the Gigafactory’s capacity sitting idle.

How Tesla’s Gigafactory bet could turn into a long-term advantage

Of course, even if the Gigafactory bet pays off, that won’t on its own justify Wall Street’s extreme optimism about Tesla. Producing 500,000 Model 3’s per year in 2018 would still leave Tesla with a fraction of its rivals’ volume, and other car companies will manage to develop their own sources of batteries sooner or later.

The question is whether Tesla can parlay the early lead provided by the Gigafactory into a strong long-term position in the electric car business.

Menahem Anderman, a battery expert at Total Battery Consulting, is skeptical. He points out that overseas battery companies, particularly LG and Samsung, are investing heavily in battery capacity now.

“If car companies want it, battery companies will install more capacity relatively quickly,” Anderman told me in a phone interview. He also said that LG has been cutting prices aggressively, potentially negating Tesla’s cost advantage.

Still, Anderman acknowledges that if demand for the Model 3 is high, Tesla will enjoy an early edge. “If utilization is high, then they'll have an advantage, though not a huge one,” he told me.

There’s reason to think an early lead could be significant. If Tesla meets its target of 500,000 vehicles in 2018, it’s going to start planning for even larger volumes in later years. Its successful experience with the first Gigafactory will give it the knowledge and confidence to place even bigger bets on additional battery factories that will come online in the early 2020s. Without that experience, other car companies could wind up always a step behind.

Apple provides a good model here. One strategy Apple has used with great success is to use big, long-term contracts to lock in the supply of key components — like LCD screens — at the lowest possible prices. Apple is able to do this because it only produces a few products per year and it has a big, loyal fan base that is practically guaranteed to buy those products in high volumes. Committing to buy millions of units is a less risky proposition for Apple than it would for other smartphone vendors that can’t easily predict which of its products will be popular.

If an early battery lead allows Tesla to sell more electric cars than anyone else in 2018 and 2019, swelling the ranks of Tesla’s fan base, that will put Tesla in a position to do something similar.

Falling battery costs mean Tesla’s economics will get better and better

Electric motors are cheaper and simpler than internal combustion engines used in conventional cars. But until now, electric cars have been more expensive than comparable gasoline-powered cars. The reason is simple: Batteries are extremely expensive. Anderman estimates that the batteries in a $35,000 Model 3 will cost around $9,000 — far more expensive than any other component.

But batteries have also been getting cheaper at a rapid clip, and experts expect progress to continue in the future. So the economics of Tesla’s cars are going to steadily improve over the next decade. Each year, the most expensive component in Tesla’s cars will get a few percentage points cheaper. The other components of a conventional car are unlikely to get significantly cheaper.

Soon we’ll reach a point where owning an electric car is actually cheaper than a comparable gasoline-powered car. One analysis from Bloomberg New Energy Finance found that the total cost of owning an electric car could become cheaper than a conventional car by 2022. And that would be great news for Tesla’s balance sheet.

Tesla skeptics like to point out that cars are a low-margin business, and so far Tesla hasn’t had much success even turning a profit on cars. But that’s because right now Tesla is selling a technology that’s inherently more expensive than the one Detroit auto makers are making. In a few years, that shoe is likely to be on the other foot: Conventional cars will actually be more expensive than electric cars, with the gap getting wider with each passing year.

If Tesla is the leading electric car maker at that point, it’s going to be in a great financial position. Its profit margins will get better and better as its costs fall more quickly than those of its competitors. It will have a lot of room to expand production as more and more customers switch away from conventional vehicles.

Correction: I originally stated that a Tesla car needs 1,000 as much battery capacity as a typical smart phone, but the correct figure is closer to 5,000. I also understated the rate at which batteries were getting cheaper.