The retail industry's landscape has certainly changed over the past four decades. Then every category of consumer goods supported several different store chains. Brick-and-mortar shopping was something of an entertainment experience as well. Now the advent of online shopping has helped narrow the total number of retailers to only a handful of category-dominant names.

That's not necessarily been a bad thing for investors, though. Shares of these select retail survivors have logged enormous gains. Costco Wholesale (COST 1.01%) comes to mind. The club-based retailer's stock is up more than 7,000% from its 1985 public offering price, as consumers have fallen in love with the idea of saving money by buying in bulk. The company's future still looks pretty bright, too.

But that gain doesn't hold a candle to the gains of another retailer that went public around that same time. Home Depot (HD 0.94%) stock is up an incredible 1,700,000% since its 1981 IPO. A $1,000 investment in the company then would be worth more than $17 million today. And that's not counting dividends. While that investment success will be a tough act to follow, Home Depot is still a powerhouse player within its sliver of the retailing world.

How can two seemingly comparable companies generate such different returns for their shareholders? There's actually a lesson here if you're on the hunt for the next company that nobody sees coming -- but everybody should see coming.

Why Home Depot outperformed Costco

Don't panic if you currently own a stake in Costco Wholesale and aren't in a position to swap it out for Home Depot shares. Both stocks are fine. It's unlikely Home Depot stock is going to continue dramatically outperforming Costco in the future anyway. In fact, Home Depot has actually lagged Costco stock's performance for the past couple of years thanks to turbulence on the homebuilding and home improvement fronts.

Still, how did Home Depot fare so much better for so much longer? You can attribute much of this disparity to the nature of the businesses themselves.

Think about it. While Home Depot and Lowe's have been around for decades, until roughly 20 years ago small and locally owned hardware stores and lumber mills were still fairly common. It was an inefficient industry ripe for disruption by mega-stores that could serve as one-stop shops for contractors and consumers alike.

That wasn't quite the case for Costco. While there's no denying there were far more locally owned and regional grocery chains in business back in the 80s and 90s, at least these stores offered the full gamut of grocery options. There wasn't as much of a game changer Costco could offer consumers on the food front.

Also bear in mind Walmart was in growth mode during the first half of this 40-year stretch. It wasn't yet the grocery powerhouse then than it's become. It was wading deeper into the grocery business then, as well as widening its other in-store offerings that would compete with Costco. Walmart was never a serious direct threat to the hardware store business at this time either, leaving companies like Home Depot the opportunity to wrestle market share away from smaller-scale hardware retailers.

Another key distinction? Not that any line of retail supports ultra-high margins, but the hardware and home improvement market is distinctly more profitable -- as a percentage of revenue -- than the grocery business. Home Depot's gross profit margin rates hover above 30%, while net income margins linger just under 10%. Costco's gross profit margin percentage is generally less than half of Home Depot's, while its net profit margin rates never get above 3%.

That's not the end of the world. After all, everything's relative. Those wider profit margins ultimately gave Home Depot something Costco could never have as much of, however -- fiscal flexibility. Home Depot simply had access to more cash flow, which could be reinvested in the business' growth, used to fund stock buybacks, and so on.

A homeowner shopping for tools in a Home Depot home improvement store.

Image source: Getty Images.

Then there's a third unintentional factor that worked in Home Depot's favor without working in Costco's. That's the way the housing market evolved over the course of the past 40 years.

Numbers from the Census Bureau indicate that in 1975 only a little over 20% of homes being built in the United States were 4-bedroom houses. Now that number is nearly 50%.

In a similar vein, citing numbers from the U.S. Census Bureau and Realtor.com, real estate broker Home Bay reports the average U.S. home's square footage has grown from 1,570 in 1980 to 2,383 square feet as of 2022. It should also come as no surprise to learn consumers are spending far more discretionary dollars on furnishing and finishing our homes now.

All of that, of course, plays into Home Depot's hand. There was no similar growth surge for the grocery and consumer goods business, however.

The past isn't the future, but...

It's an interesting anecdotal story to be sure. But it's also all in the past. Both the grocery markets as well as the home improvement markets are now pretty well saturated. Neither stock will be dishing out a repeat performance over the course of the next 40 years.

Still, it's difficult to argue that the discretionary, cyclical nature of Home Depot's business continues to make it the higher-potential pick of the two. Just buckle up for the greater volatility that comes with being a discretionary name rather than a more stable consumer staples stock.

More than anything, though, investors should understand that a once-in-a-lifetime megatrend that materialized for a higher-margin business explains Home Depot stock's significant outperformance of Costco shares. These are the sorts of things investors should be thinking about -- and looking for -- when looking for any new long-term picks.