NEW YORK — If you'd told investors what was going to happen in 2012 – U.S. economic growth at stall speed, an intensifying European debt crisis, a slowdown in China, fiscal deadlock in Washington, decelerating corporate earnings growth – and asked how the stock market would perform, few would have predicted a good year.

But that's just what they got.

The Dow Jones industrial average, the Standard & Poor's 500 and the Nasdaq composite index will all end the year substantially higher, despite losing ground in the final days of the year as concerns about the looming "fiscal cliff" mounted.

The Dow is on track for a 7 percent increase, its fourth yearly gain in a row, having started the year at 12,217. The S&P 500, which started the year at 1,257, is up 12 percent, beating the 7.8 percent average annual gain of the past 20 years. The Nasdaq also logged a better-than-average gain, 14 percent.

Those returns were higher when dividend payments were taken into consideration. On that basis the Dow returned 10 percent, the S&P 500 index 15 percent and the Nasdaq 16 percent.

"There's been a lot thrown at this market, and it's proven to be very resilient," said Gary Flam, a portfolio manager at Bel Air Investment Advisors in California. "Here we are at the end of the year, and it's still relatively strong."

Stocks started the year on a tear, with optimism about an improving job market and a broader economic recovery providing the backdrop to the S&P 500's best first-quarter rally in 14 years.

The index advanced 12 percent by the end of March, closing the quarter at 1,408, its highest in almost four years, with financial companies and technology firms leading the charge. The Dow ended the first quarter at 13,212, logging an 8 percent gain.