What happened

Hershey (HSY 0.57%) shareholders are trouncing the market this year. The stock rose 14% in the first half of 2022, according to data provided by S&P Global Market Intelligence, compared to a 21% drop in the S&P 500.

That rally looks even better when you zoom out a bit, with shares up 26% in the past full year compared to a 12% decline in the wider market. The confectioner is also beating the S&P 500 over the past three years. Hershey's performance can be traced directly to its strong operating performance at a time when many investors are seeking more stability and dividend income.

So what

The company's last earnings report illustrated why Wall Street is so bullish on the sweets and snacks business. Organic sales jumped 12% in the quarter than ended in late April, putting Hershey near PepsiCo (PEP 3.62%), another outperforming stock.

Consumers are spending freely on its snacks and sugary treats despite the extra pressure from inflation. "We delivered double-digit sales and earnings growth with ongoing momentum across business units," CEO Michele Buck said in late April.

The stock's rally has also been supported by Hershey's clear pricing power. Gross profit margin rose to 46.7% of sales from 45.7% this past quarter, meaning that price increases more than offset soaring expenses in areas like labor and raw material inputs. Hershey's operating profit expanded at an even faster rate as the company spent more efficiently in areas like marketing and advertising.

Now what

Hershey's rally has many investors questioning whether a pullback might be on the way. It is valued at a huge premium of nearly five times annual sales, after all, compared to PepsiCo's three times annual sales.

HSY PS Ratio Chart

HSY PS Ratio data by YCharts

Pepsi is a bigger, more diverse business that is growing more quickly and has a longer track record of dividend increases behind it. Those differences suggest that Hershey stock could have room to fall toward Pepsi's valuation, especially if growth trends worsen. It's possible that the next round of price increases won't be as favorably received by consumers, after all.

Yet Hershey is far more profitable than Pepsi and most of its other packaged food peers. And its profitability has been rising lately, rather than declining. If the company can protect that positive momentum, then the stock might extend its rally deeper into 2022.