In late 2012, shares of consumer electronics giant Best Buy (BBY -0.30%) -- which had traded around $50 before the Great Recession -- plunged below $12. Many analysts and pundits opined that Best Buy was doomed because of the rise of "showrooming." In short, customers might continue to visit Best Buy to look at products, but they would then go online to order from Amazon.com (AMZN -1.14%), which often had lower prices.

Yet Best Buy has come roaring back in recent years. Price-matching, deep cost cuts, store rationalization, and e-commerce investments have allowed it to fend off the threat from Amazon. As a result, Best Buy stock has quadrupled since bottoming out four years ago.

BBY Chart

Best Buy Stock Performance, data by YCharts.

Right now, investors seem to be just as pessimistic about Macy's (M 0.16%) as they were about Best Buy a few years ago. However, Best Buy's comeback shows that Macy's may be able to turn things around.

How did Best Buy do it?

In the past year, Best Buy has delivered a series of strong earnings reports, consistently beating analysts' earnings estimates. Just last week, the company reported that its adjusted earnings per share had surged 51% year over year in Q3.

There have been three key aspects to Best Buy's recovery strategy: 1. transformation of the store base, 2. deep cost cuts, and 3. investments to compete with Amazon and other online retailers.

First, Best Buy has closed big-box stores at a slow but steady pace. Best Buy ended last year with 1,037 big-box stores in the U.S., down from more than 1,100 in early 2012. Meanwhile, Best Buy has made the stores more productive by building a variety of brand-focused mini-shops within each store. These shops have employees who can offer expert brand-specific advice, differentiating Best Buy from other retailers.

Best Buy has rationalized its store base in the last few years. Image source: The Motley Fool.

Second, Best Buy has implemented a series of deep expense cuts as part of its "Renew Blue" turnaround plan. The company initially aimed to achieve $725 million in annual cost savings. But by early 2015, it had already surpassed $1 billion in savings on an annualized basis, and it is now well on the way to reducing costs by another $400 million a year.

Third, Best Buy has become more aggressive in competing with Amazon. A few years ago, it introduced a price-matching program to assure customers that they wouldn't overpay. Best Buy has also lowered its prices and invested in improving its e-commerce site. These efforts are paying off. Domestic online revenue surged 24.1% last quarter.

Macy's is following many of the same strategies

Macy's is trying many of the same things as it pursues its own turnaround plan. In early 2016, it announced plans to reduce its operating expenses by $500 million over the next few years.

Macy's is also closing stores even more aggressively than Best Buy. In the past year, it has announced roughly 140 store closures, affecting nearly 20% of Macy's full-line stores. Some of these stores shut their doors earlier this year, but most will close in early 2017.

At the same time, Macy's is doubling down on its own strategy of opening brand-oriented shops within its stores. For example, Macy's will open 280 LensCrafters shops within its stores by the end of 2017. It is also bringing the Bluemercury beauty chain that it bought last year into some Macy's stores. Macy's is even looking into opening more restaurants within its stores to drive higher customer traffic.

Macy's wants to open more shops and restaurants within its stores. Image source: The Motley Fool.

Meanwhile, Macy's is already a formidable e-commerce player. It gets about 20% of its revenue from online sales, and it is investing heavily to position itself for growth in mobile commerce. Macy's is still dwarfed in size by Amazon.com, but it has made one of the most successful transitions to e-commerce among brick-and-mortar retailers.

All is not lost

In the past year or two, it has become fashionable (again) to proclaim the death of the department store. It's easy to reach that conclusion by looking at the last few quarters of results, as Macy's has seen a sharp contraction in its profitability.

Yet less than two years ago, Macy's profitability had reached record levels, after six consecutive years of consistent sales growth and double-digit earnings-per-share growth.

Macy's and other department stores clearly face headwinds from changing consumer behavior and the growth of Amazon. But Best Buy has shown in the past few years that it's possible to overcome these threats by cutting costs, closing and/or redesigning stores, and competing more aggressively. It is way too early to count Macy's out.