Fast food stocks delivered a wide range of returns for investors this year as companies battled for customer traffic in this highly competitive industry. A few well-known chains, including Chipotle (CMG -2.02%), underperformed by a wide margin. Others, like Panera, left the public markets altogether in favor of going private.

The stocks that trounced the S&P 500 did so by posting a mix of healthy sales and profitability gains. Let's take a closer look at the three best performers in the sector.

3. Yum Brands

Yum Brands (YUM -0.96%) outpaced the broader market by 10 percentage points this year as it showed off the power of a diversified approach to fast food. Sure, the Pizza Hut chain struggled with sales declines and a modest drop in profitability in 2017. However, those dips were more than offset by gains in Yum Brands' other chains.

A man eating fried chicken.

Image source: Getty Images.

Taco Bell's comparable-store sales growth sped up to a 5% pace through the first three quarters of the year from 1% in the prior year period. KFC, its biggest and most profitable division, also accelerated its growth pace even as restaurant-level margin rose to 15.1% of sales from 14.4%.

Yum Brands combined these successes at existing locations with aggressive store growth, especially in international markets. Altogether, these initiatives pushed operating profit higher by double digits in the most recent quarter. CEO Greg Creed took that opportunity to affirm executives' full-year guidance that calls for high single-digit profit growth in 2017, or just a minor slowdown from the prior year's 11% increase.

2. Shake Shack

Burger joint Shake Shack (SHAK -1.57%) underperformed the market for most of the year before racing higher in 2017's closing weeks. Wall Street has recently become more optimistic about its growth potential, given that executives have announced plans to scale up new store openings. The chain's already aggressive expansion pace delivered 27% sales growth in the third quarter, which is a rate that more established companies can't hope to approach.

A man takes a bite out of a burger.

Image source: Getty Images.

The problem is that Shake Shack isn't posting growth where it counts: at its existing locations. In fact, comparable-store sales fell 1.6% last quarter and are on pace to shrink by between 1.5% and 2% for the full year. Customer traffic trends aren't encouraging, either, as the company handled 4% fewer guests at its established stores in the third quarter.

For Shake Shack's recent positive momentum to carry on into 2018, investors will likely need to see improvements in comps and in customer traffic trends. Otherwise, the company won't have much of a foundation to support even modest store expansion goals.

1. McDonald's

It's no exaggeration to say that McDonald's (MCD -0.74%) had an excellent 2017. The fast-food titan ended its two-year sales slide in dramatic fashion as it posted back-to-back comps gains of 6% or better. That success included a fiscal second-quarter gain that marked its best expansion rate in over five years. Mickey D's is enjoying consistently positive -- and accelerating -- customer traffic, which suggests it is recapturing the market share that it had ceded to rivals like Chipotle and Shake Shack in the past few years.

Four young adults share a fast food meal.

Image source: Getty Images.

McDonald's plans to tighten its hold on its leadership position through more menu innovations and an aggressive move into digital ordering and home delivery in 2018. Over the longer term, shareholders can expect profitability to continue setting new highs at least through 2019. That's when management is aiming to push operating margin up to the mid 40's, up from 35% last year, with help from its refranchising initiative.

Given its improving profitability and customer traffic, Mickey D's appears to have the best shot at beating the market again in 2018. Sure, smaller rivals might enjoy periods of faster growth, but McDonald's recent results show just how difficult it is to win -- and keep -- market share from the industry titan.