In the past decade, the S&P 500 and the Nasdaq Composite index have produced total returns, including dividends, of 225% and 323%, respectively. These gains prove that investing in the stock market is a fantastic way of building wealth.

Unsurprisingly, some companies have fared much better. Your mind might immediately go to the dominant "Magnificent Seven" stocks, which is understandable.

But it might shock you to learn that O'Reilly Automotive (ORLY 0.67%), a leading aftermarket auto parts retailer, has seen its shares skyrocket 657% in the past 10 years, turning a $10,000 investment into more than $75,000 today (as of April 17). Should you hop on the bandwagon and buy this winning stock?

Creating value for shareholders

Investors will find no shortage of reasons why O'Reilly Automotive is such a wonderful company. There's no doubt that these positive attributes have contributed to its stock's performance.

For starters, the business continues to benefit from meaningful expansion opportunities. O'Reilly currently has 6,157 stores after opening 186 in 2023. The plan is to open 195 more this year. The industry is extremely fragmented, giving the company plenty of opportunity to lean on its scale and brand to gain market share.

There are two other powerful industry trends that will keep propelling O'Reilly. One is the fact that each and every year, the number of cars on the road goes up. Moreover, these vehicles are getting older, which leads to greater demand for O'Reilly's products and services.

Speaking of demand dynamics, investors will appreciate how recession-resilient this company is. Consumers need their cars to work no matter what, whether we're in good or bad economic times. This situation drastically adds stability and predictability to the business model.

From a financial perspective, O'Reilly has been able to steadily increase its top and bottom lines. In the past 10 years, revenue and diluted earnings per share have risen at compound annual rates of 9% and 20%, respectively. Given the factors I just outlined, there's a good chance this kind of performance can continue for the foreseeable future.

Even after investing capital toward opening more stores, O'Reilly generates more cash than it knows what to do with. That's why management aggressively repurchases shares, spending $3.2 billion to buy back the stock in 2023.

In the past five years, the outstanding share count has been reduced by a jaw-dropping 26%. This means longtime investors constantly see their ownership stakes rise over time.

What about the valuation?

After a business puts up an outstanding 657% return in the past decade, investors are right to wonder if the shares still present a compelling buying opportunity. It's best when a stock is purchased below its intrinsic value, as the potential for outsized forward returns is still present and expectations haven't gotten too frothy.

As of this writing, shares of O'Reilly trade at a price-to-earnings (P/E) ratio of 28.4. This is about as expensive as the stock has been in the last eight or so years. And it represents a sizable premium to the trailing five- and 10-year average P/E multiples. So you can make a valid argument that shares are overvalued right now.

Investors might be persuaded to wait for a much better entry price. That makes sense as a rational way of thinking, but perhaps the best strategy if you're bullish on this business is to dollar-cost average in the stock over several months. This will allow you to buy at multiple entry points.

Given the superior quality of O'Reilly, it could be a winner in your portfolio over the next several years.