Investing in the stock market can be extremely frustrating for those who look back on the big rally they missed and nerve-wracking for those who believe the may be putting money into the market at the top.

The S&P 500 has roared 177% from its March 9, 2009 low of 666 to its January 2014 high of 1,850.

Thanks to scary USA Today cover stories and increasing amounts of crash chatter, everyone is worrying that we are at or near a market peak. And this has investors extremely hesitant to buy stocks for fear of a big decline or perhaps a crash.

Obsessing over the risk of a crash could lead to analysis paralysis. However, there is a very basic investing strategy that can save investors from losing too much hair as they make the decision to buy stocks.

It's called dollar-cost averaging.

There's A Correct Way To Buy Stocks As The Market Is Crashing

The stock market is great for investors who have the benefit of long-term investing horizons. It's also better-suited for investors who aren't concerned about perfectly-timing market tops and bottoms.

Having said that, taking a longer term view is good for investors worried that they may be buying at the top of the market.

A strategy called dollar-cost averaging can help reduce risks surrounding an asset falling in price. The concept is straightforward - you invest a fixed amount of money in an asset once every fixed time period. If the asset's price drops, you will be getting more shares of the asset for the same amount of money, and so if and when the price recovers, you will have spent less per share, on average, than if you had bought the shares at their peak, pre-fall price.

Dollar-cost averaging isn't about losing money as the stock market falls. It's about buying increasing amounts of shares at cheaper prices, which means bigger returns during the rally.

How Dollar-Cost Averaging Worked Brilliantly During The Last Crash

To see this in action, we came up with a simplified thought experiment.

We considered what would have happened to an investor jumping into the stock market at the last peak: October 2007. This was arguably the worst time to buy. Our hypothetical investor puts $50 into a S&P 500 index fund every month, starting in October 2007 — the last stock market peak before the beginning of the great recession.

Here is what happened to the S&P 500 starting at that peak:

Business Insider/ Andy Kiersz

The index dropped more or less steadily until the worst moments of the financial crisis in fall 2008, causing the full on crash, and only began to turn around in March 2009.

The key to our investor's experiment is that they are staying consistent. No matter how stock prices move, they will always put $50 every month into the index fund.

Based on changes in the value of the S&P 500 index, we calculated our investor's price return, less the $50 monthly cost:

Business Insider/ Andy Kiersz

The value of our investor's portfolio as of January 2014 is $5631.87. If they instead had taken their $50 each month and held it as cash, they would have just $3800. So, the price return on this investment — even though they started at the last peak, just before the market started to go downhill — is $1831.87.

This is a respectable 48% return. That averages out to about a 7.6% annual rate of return.

To get another perspective on this, here is the percent gain or loss, compared to taking $50 each month and holding it as cash:

Business Insider/ Andy Kiersz

Things start out looking pretty dire, as the economy fell into its deep recession through mid-2009, with the S&P 500 reaching a minimum in March of that year. At the low point, in March 2009, our investor would have been down about 34% compared to just holding cash.

Because human beings are often overly risk-averse, our hypothetical investor might have been tempted to abandon their investment plans during the bad months. That is, they might look at this chart and panic about the drop:

Business Insider/ Andy Kiersz

But, if our investor sticks with their plan and keeps putting $50 in every month, even through the dark times, once the market bounces back, they end up doing quite well:

Business Insider/ Andy Kiersz

Here's Why You Never Hear About This

Unfortunately, dollar-cost averaging isn't sexy. It's much sexier to sell at the top and buy at the bottom.

Obviously, your returns would be much higher if you win the stock market lottery by perfectly timing the tops and bottoms of the market. However, almost everyone who tries to do this will find themselves losing money and lots of it.

If you are investing for the long haul, and can hang on through watching your portfolio's value drop temporarily in bad times, starting to invest in stocks, even near a peak, may not be as terrifying as it looks. The market has always bounced back sooner or later, so if you can hold on until that later, don't panic.