Few stocks have outperformed Amazon.com (AMZN -2.76%) since the company started trading shares publicly on the Nasdaq. The e-commerce giant is now the fifth largest company in the S&P 500 by market cap, worth around $404 billion. But Amazon wasn't always a megacap company that sold everything from A to Z.

Back when the company had its IPO in 1997, Amazon was pretty much just an online bookstore. The company sold its shares for just $18 to raise $54 million at a valuation of $438 million. Today, it takes Amazon about two days to raise that much cash (based on its 2016 free cash flow).

If you had been able to buy $2,000 of Amazon's IPO and held on to it for the past 20 years, you'd be sitting on about $1,136,000 in Amazon stock today. That's a return of 56,821%, or an average of around 37% annually. Even if you'd bought shares about four months later, like Fool co-founder David Gardner, you'd still see around half the returns.

Table with Amazon.com logo and assortment of stuff Amazon sells.

Image source: Amazon.com.

Not for the faint of heart

Anyone who achieved the staggering returns that were possible from investing in Amazon's IPO must have had very strong conviction in the company. Shares of the stock lost 6% of their value in a single day on 199 separate occasions. The share price fell 15% over a three-day period 107 different times.

If you bought shares at the IPO, you were probably flying high as the dot-com bubble pushed shares up to $113 at the end of 1999. And that was after a series of splits that gave IPO investors 12 shares for every one they originally purchased. But then shares came crashing down as low as $5.67 per share in September 2001.

Amazon stock lost more than half of its value on three other occasions: between April and August in 1999, from October 2003 to August 2006, and from December 2007 to November 2008.

Anyone who held on through those major swings deserves the phenomenal returns he or she achieved.

How do you hold on?

So the question isn't as much how to find the next Amazon, but how do you hold on to it when you think you might have found it.

David Gardner says the biggest factor that allowed him to hold on to the stock after watching the share price crash 95% was "looking at the business, not the stock." Paying attention to the underlying fundamentals and execution of a company can help investors stay rational when the market becomes irrational.

Additionally, having a reason for investing in a company -- a thesis -- is key. With Amazon, the thesis may be that e-commerce is going to be a big part of how we shop in the future, and Amazon is in the best position to capture a big part of that market. As long as that thesis remains intact, there's probably no reason to sell the stock.

While you could have produced an average return of 37% over the past 20 years from an investment in Amazon, it takes a certain kind of investor to achieve those results.