The last couple of years have been tough for the stock market, but in recent months, prices have been thriving. The S&P 500 is up by more than 24% over the past year, while the tech-heavy Nasdaq has soared by a whopping 43% in that time.

However, many investors are concerned about how long this surge might last. While some experts are claiming that this is the beginning of a new bull market, others are worried that this may only be a temporary rally before prices fall again.

Because nobody can say for certain where the market is headed, it's tough to know whether to invest now or wait. That said, there is one stock-market figure that has a flawless track record -- and it has good news about the future.

Bull silhouette against an orange sky.

Image source: Getty Images.

Is it safe to invest in the stock market right now?

First, it's important to note that past performance isn't indicative of future returns. In other words, although this stock-market indicator has been positive in the past, that doesn't necessarily mean it will continue that trend in the future.

However, it can still be helpful to see how the market has performed over time. Analysts at Crestmont Research examined the S&P 500's rolling 20-year total returns dating back to 1919 to see how many of those periods ended in positive returns for the index.

They found that in all 104 of those periods, from 1919 to 2022, the S&P 500 earned positive total returns. This means that if you had invested in an S&P 500 index fund or ETF at any point and simply held it for 20 years, you'd have made money -- no matter how volatile the market was in that time.

Even in the last two decades alone, the market has thrived despite experiencing some of the worst recessions and bear markets in history.

^SPX Chart

^SPX data by YCharts

Since 2000, the market has faced the dot-com bubble burst, the Great Recession, the COVID-19 crash, the most recent slump, as well as countless smaller downturns along the way. Yet the S&P 500 is still up by nearly 225% in that time.

What does this mean for you?

The market's short-term performance will always be unpredictable to a degree. Even the experts can't say where stock prices will be a week or a month from now. But over decades, research shows that the market is incredibly consistent.

Rather than trying to invest at the perfect moment, then, it's often safer to invest whenever you can and keep a long-term outlook. Even if the market takes a turn for the worse in the near term, chances are it will still go on to see positive returns over the next decade or two.

Even investing at a "bad" moment will still often work out over time. For example, say you had invested in an S&P 500-tracking fund in 2000 -- almost immediately before the market plummeted during the tech-bubble burst. At the time, that may have seemed like the worst possible moment to buy. But if you'd simply stayed invested, you'd have more than tripled your money by today.

The key to long-term growth

Keeping a long-term outlook is important, but it's equally crucial to ensure you're investing in the right places. The strongest stocks come from healthy companies, and these investments are the most likely to recover from downturns and go on to experience long-term growth.

While there are never any guarantees when investing, the best stocks are from businesses with solid underlying fundamentals, which include everything from healthy financials to a knowledgeable leadership team to a competitive advantage in the industry.

Nobody can say for certain what will happen with the market in the coming weeks and months. But by investing in quality stocks and holding those investments for the long term, you can rest easier knowing your portfolio is as protected as possible.