The problem for Macy's investors was how much money they already made off the company over the previous 12 months. Before the department store chain reported earnings Wednesday, its shares had more than doubled, from $20.29 a year ago to a close of $41.82 Tuesday. Even with this week's drubbing, Macy's shares are still up more than 82 percent over the last year and 42 percent since the beginning of the year.

"Macy's struck me by how positive they were, and it was a very upbeat earnings call," Retail Metrics founder Ken Perkins told CNBC.

While the profit margins at Macy's were slimmer than investors liked, there wasn't anything buried in its earnings that spelled trouble.

Macy's shares spun into an almost free fall, closing down 16 percent for the day while Walmart rallied up 9.3 percent .

The sales, profit and individual store sales growth at both retailers were better than Wall Street analysts expected, and Macy's even raised its forecast through the end of the year.

Walmart and Macy's both had exceptionally good news for investors when they released their second-quarter earnings this week.

"They had already had a good run," so investors were largely taking some profits from Macy's this week, Perkins said.

Walmart's shares had a decent run too, but not anywhere as good as Macy's. The Bentonville, Arkansas, retailer's shares rose 11.7 percent over the same time frame and are up almost 25 percent over the last 12 months.

The sell-off in Macy's shares this week should be viewed as "just a breather" from the long runup already this year, Jefferies analyst Randal Konik told investors in a research note Thursday. "Yesterday's sea of red on [Macy's] results presents a major buying opportunity for the group."

The retail sector as a whole is benefiting from strong consumer confidence throughout the U.S. and record low unemployment rates. More shoppers are opening their wallets and are expected to continue to do so through this holiday season. Retail sales for the year are now expected to rise more than previously predicted, the National Retail Federation said earlier this week. One cloud hanging over the industry is potential additional tariffs implemented by President Donald Trump's administration, but it's still uncertain what impact that could have on companies such as Macy's and Walmart, if any.

The two retailers' results provide more evidence that the consumer is roaring back and spending more than they have since before the recession. Konik said consumers are "the strongest they have been since 1999."

Investors also rewarded Walmart more because they see more stability in the retailer's real estate strategy, while Macy's will likely have to close more stores, analysts said. Uncertainty around when that will happen and what Macy's will do with its remaining locations has spooked investors before. Both companies have been looking for ways to fill excess square footage within their stores — Walmart has partnered with FedEx to open service centers, for example, while Macy's leases space to Starbucks for cafes.

Both companies also are pouring money into their digital sales, remodeling stores and adding more in-house brands.

The expenses have added up and eroded profit margins, although Walmart appears to be further along in the process, analysts said. Both retailers are also trying to stay competitive on price — Macy's, for example, is adding more discount locations within its stores across the U.S.

"The erosion of profitability and margins are necessary evils," said Neil Saunders, GlobalData retail managing director.

Perkins said it's tough for investors to wait for those investments to pay off.

"The margin pressure is going to be there for these guys," he said. "And everybody wants a quick buck."

Looking to the full year, both Macy's and Walmart are anticipating a strong holiday season as consumers regain confidence. Nordstrom should be another beneficiary of this and also hiked its full-year outlook Thursday, which sent shares soaring more than 13 percent on the news.

Retailers including Target, Kohl's, TJX, Lowe's, Gap and L Brands are set to report earnings next week, which should offer a better picture of how the industry is positioned ahead of the second half of the year, and how investors are responding. Many retail stocks have rallied over the past year — the S&P 500 Retail ETF is up more than 32 percent.

Investors will continue to look for any pressure on profit margins, e-commerce sales growth, and with some of these companies potentially additional store closure announcements.