In the past few months, numerous major retailers have seen their share prices undermined by concerns about competition from e-commerce juggernaut (AMZN -0.32%).

Costco Wholesale (COST 1.11%) stock plunged after Amazon announced its intent to expand in the grocery business by buying Whole Foods. Home Depot (HD -0.03%) stock fell earlier this month on the news that Sears Holdings would begin selling Kenmore appliances through Amazon. And while Macy's (M 1.10%) has been posting poor results lately, its stock price has plummeted in a manner out of proportion to its sales declines.

M Chart

Costco Wholesale, Home Depot, and Macy's stock performance; data by YCharts.

The example of Best Buy (BBY 0.41%) shows that these fears may be overblown. Five years ago, it seemed to many people that Amazon would drive the big-box consumer-electronics giant out of business. Yet Best Buy has found ways to survive and even thrive in the face of competition from Amazon.

Best Buy got knocked down -- but survived

In 2012, Best Buy seemed as if it would become one of the first victims of Amazon's torrid growth. During Best Buy's 2013 fiscal year -- which roughly corresponds to the 2012 calendar year -- comp sales fell 1.7% in the domestic market and plunged 7.5% in the international market.

Because of the tough competitive environment, Best Buy's adjusted operating margin declined to 3% in fiscal 2013 from 4.6% a year earlier. As a result, adjusted EPS plunged from $3.61 to $2.62. These discouraging results caused Best Buy stock to crater, bottoming out around $11 -- down by about 75% from late 2010.

Comp sales fell again in both the domestic and international markets during fiscal 2014, leading to another operating margin decline. This seemed to confirm investors' dour outlook for Best Buy.

Indeed, the company's revenue has never recovered from these setbacks. Still, in the past few years, Best Buy has managed to stabilize its revenue and rebuild its operating margin. As a result, the stock has soared, briefly hitting a new all-time high above $60 earlier this year.

BBY Chart

Best Buy Stock Performance, data by YCharts.

How Best Buy came out alive

Compared with the likes of Costco Wholesale, Home Depot, and even Macy's, Best Buy didn't have much going for it in competing with Amazon. Costco has unbeatable prices, because of its focus on selling huge quantities of a relatively small selection of items. Home Depot and Costco both sell a lot of bulky items that are tough to sell online profitably. Meanwhile, Macy's and Home Depot both carry lots of items that customers want to see or try before they buy.

By contrast, with the exception of its appliance business, Best Buy mainly sells products that are ideal for e-commerce disruption. Consumer-electronics products typically have a fairly high value relative to their size and weight, so shipping costs aren't prohibitive. Meanwhile, Amazon can often sell the same products Best Buy carries in its stores.

Best Buy fought back by offering to match competitors' prices, including those of Amazon. As a result, customers who wandered into a Best Buy store to look at merchandise no longer had any reason to order from Amazon rather than making a purchase then and there.

The exterior of a Best Buy store

Cost cuts and price-matching were the keys to Best Buy's turnaround. Image source: Best Buy.

Some analysts thought price-matching would destroy Best Buy's profit margin. The company headed off that concern by implementing a broad-based cost-cutting program. It also pulled out of international markets where it was struggling.

As a result, Best Buy's revenue has fallen below $40 billion, compared with more than $50 billion at its peak. Nevertheless, its operating margin rebounded to 4.7% last year -- the company's best result since fiscal 2011 -- driving strong earnings growth. Analysts expect further EPS growth this year and next year.

A buying opportunity?

Between 2011 and 2016, nearly tripled its sales, from $48 billion to $136 billion. Revenue is still growing more than 20% annually despite Amazon's massive size. Amazon is thus capturing a huge proportion of the growth in retail sales, at least in the United States. This development will clearly affect a wide swath of the retail universe.

Nevertheless, Best Buy's resurgence over the past five years shows there is hope for companies that lead their respective industries -- such as Costco Wholesale, Home Depot, and Macy's.

Home Depot and Costco are still generating strong comp sales growth, which suggests that they may be more immune to competition from Amazon than some investors believe. But even if they do eventually face greater pressure on sales, Best Buy's example shows that they could continue to produce strong earnings by committing to keep costs down.

As for Macy's, many investors seem to think the department-store giant is doomed, because it's set to report a third straight year of declining sales in 2017. However, if Best Buy could come back from several years of falling sales and plunging margins, there's no reason Macy's can't do so as well.