With inflation soaring and consumers spending less, the pandemic-inspired preference for cooking at home has persisted. That's good for packaged food companies. The $1.03 trillion U.S. packaged food market is projected to grow at an annual rate of nearly 5% through 2030.

But it's not all smooth sailing. To cushion margins from increased labor and transportation costs, packaged food suppliers have been steadily raising prices. The risk is that consumers can quickly change behavior based on price hikes. Let's take a look at two packaged food titans and where each is headed.

Kraft Heinz

The Kraft Heinz Company (KHC -0.55%) operates the third-largest food and beverage company in North America and the fifth-largest in the world. With 2021 net sales of approximately $26 billion, Kraft comprises more than 200 brands loved by grocery shoppers around the world, including Oscar Mayer, Ore-Ida, Kool-Aid, Velveeta, and Philadelphia Cream Cheese.

Oscar Mayer Wienermobile with foggy vista as a backdrop.

Image Source: The Kraft Heinz Company.

Co-headquartered in Chicago and Pittsburgh, Kraft Heinz sells its products in nearly 200 countries across the globe and retains employees in over 40 countries. The pandemic has challenged Kraft Heinz considerably, primarily with persistent supply chain disruptions and price increases. As a result, the mac & cheese magnate's Q2 margins dropped to 30.3%, down over four percentage points from the same period last year.

To offset narrowed margins, Kraft implemented several rounds of price hikes, raising prices an average of 12.4% overall. But the results have been less than desirable. The Lunchables maker noted a slight slowdown in demand due to the higher prices, and a key indicator for sales volumes dropped 2.3% in the second quarter.

The burning question on investors' minds remains this: With Kraft Heinz stock trading roughly 63% down from its highs of 2017, is all of this bad news already priced in? CEO Miguel Patricio claims that 99% of the company's price hikes have already been implemented or announced, and that any additional increases will be "surgical."

Combined with the fact that Kraft just raised its 2022 revenue growth forecast from a low- to a high single-digit percentage, this stock is likely to see some upside recovery.

General Mills

General Mills (GIS -0.77%) not only makes many of the world's favorite cereals like Lucky Charms and Cheerios, but also operates other food brands including Betty Crocker, Yoplait, Annie's, and Blue Buffalo pet foods. The company's more-than-100 food brands stack up to a major presence in the grocery store.

After a strong fiscal first quarter, the Cinnamon Toast Crunch maker recently raised its full-year outlook for organic net sales, earnings-per-share growth, and operating profit. Outlook for organic sales growth and diluted EPS growth were both raised 2%, a sign of confidence for the three remaining fiscal quarters. And General Mills anticipates full-year operating profit to grow as much as 3%.

Just as it has at other food purveyors, the high-inflation environment has also impacted General Mills. While the company has enjoyed a lasting consumer trend of cooking and snacking at home, implementing price increases in accordance with higher operating costs has been tricky. On one hand, the company needs to make up for shrinking margins, but on the other, there is the risk of customers trading down to non-branded alternatives.

Despite challenges, investors seem to like what General Mills is doing. The stock recently printed new all-time highs for six months in a row, from April through September. This presents a much different investment scenario than Kraft stock, which is trading far below its all-time high. 

So should investors buy the dip on Kraft or buy the highs on General Mills?

To gauge which stock is the better buy, let's compare market capitalizations, price-to-earnings ratios, and dividend yields.

Metric The Kraft Heinz Company General Mills
Market cap $44.2 billion $46.5 billion
Price-to-earnings ratio 34.87 16.46
Dividend yield 4.44% 2.76%

Data source: E-Trade

A much lower price-to-earnings ratio makes General Mills a more enticing buy, but Kraft Heinz's superior dividend yield is also attractive to investors. While General Mills might be the better near-term buy, both of these consumer staples stocks present long-term potential for investors, and both look to be solid investments in the years to come.