Costco (COST -0.73%) and Dollar General (DG 0.99%) are both resilient retailers. Both companies opened new stores as other brick-and-mortar stores succumbed to the ongoing retail apocalypse, and they handily weathered the COVID-19 pandemic over the past year.

Over the past 12 months, shares of both companies rose more than 30% as the S&P 500 advanced about 16%. Let's see why Costco and Dollar General outperformed the broader market last year, and see if one is a better overall investment today.

A woman in a mask checks her phone as she shops in a store.

Image source: Getty Images.

How did Costco and Dollar General survive and thrive?

The retail apocalypse, which started after the 2008-09 Great Recession, was mainly caused by the rise of Amazon (AMZN 2.19%) and other e-commerce options, the expansion of superstores like Walmart (WMT -0.60%), and the over-expansion of malls and other brick-and-mortar businesses in urban and suburban areas.

Costco's warehouse club business, which generates most of its profits from membership fees instead of product sales, was shielded from those headwinds for two reasons. First, its products were generally cheaper when purchased in bulk, especially after factoring in shipping fees from online marketplaces. Second, its membership platform locked in shoppers.

Dollar General isn't a true dollar store, but it still sells most of its products at lower prices than Walmart, Amazon, and other large retailers, which makes it a top shopping destination during economic downturns. It opens most of its stores in rural areas instead of urban and suburban areas, which helps it avoid direct competition with superstores and other retailers.

When the pandemic started, many shoppers flocked to both retailers to stock up on essential products. That trend helped Costco and Dollar General grow as smaller brick-and-mortar businesses shut down.

Both companies opened new stores throughout the crisis. Costco ended its latest quarter with 803 warehouses worldwide, up from 785 locations a year ago. Dollar General ended its latest quarter with 16,979 stores in 46 states, up from 16,094 stores a year earlier.

How fast is Costco growing?

Costco's adjusted comparable store sales grew 9.2% in fiscal 2020, which ended on Aug. 30, with 9.2% growth in the U.S., 7.4% growth in Canada, and 11.2% growth across its other international markets. Its e-commerce comps increased by 50.1%.

A grocery cart in a supermarket aisle.

Image source: Getty Images.

In the first quarter of 2021, Costco's adjusted comps rose 17.1% year over year, with 17% growth in the U.S., 16.8% growth in Canada, and 17.7% growth in its other international markets. Its e-commerce comps surged 86.2%. It mainly attributed that acceleration to pandemic-related tailwinds.

Costco's renewal rate remains above 90% in the U.S. and Canada, and in the high 80s internationally. Those high renewal rates indicate its shoppers are fiercely loyal, and that should prevent it from losing ground to Amazon, Walmart's Sam's Club, and other competitors.

Analysts expect Costco's revenue and earnings to rise 10% and 15%, respectively, this year. Next year, they expect its revenue and earnings to grow 6% and 9%, respectively, as it faces tougher post-pandemic comparisons.

How fast is Dollar General growing?

Dollar General's same-store sales rose 3.9% in fiscal 2019, which ended on Jan. 31, marking its 30th straight year of positive same-store sales. In the first nine months of 2020, its same-store sales grew another 17.5% as a pandemic-induced spike in the average ticket size offset a slight decline in its in-store traffic.

Like Costco, Dollar General attributed that acceleration to the pandemic, which sparked robust sales of its consumables, seasonal products, home products, and apparel. During that period, it posted its strongest year-over-year growth in home product sales ever.

Dollar General's gross margin also expanded year over year during those nine months, thanks to fewer markdowns and a growing mix of higher-margin non-consumables. That trend should allay concerns about competition or tariffs on Chinese goods weighing down its margins.

Analysts expect Dollar General's revenue and earnings to rise 21% and 59%, respectively, this year. Next year, they expect its revenue to rise just 1%, and for its earnings to dip 6% on tougher year-over-year comparisons.

Costco trades at 38 times forward earnings, while Dollar General has a much lower forward P/E ratio of 22. Both stocks pay forward dividend yields of about 0.7%, which probably won't attract serious income investors.

The winner: Dollar General

I believe Costco and Dollar General are both solid long-term retail investments. Both companies have loyal customer bases, and their businesses are naturally insulated from Amazon and Walmart.

However, Costco's high P/E ratio will likely limit its gains this year, especially as the pandemic passes and its growth decelerates. Therefore, I'd pick Dollar General right now for its lower valuation, and only consider accumulating some shares of Costco after its valuations cool off.