Investing in a Roth IRA is a smart way to save for retirement, particularly if you're concerned about your future tax bill. But the stock market doesn't exactly look like a bargain right now. Despite taking a tumble earlier this week, the S&P 500 index is still up about 18% year to date.

With stocks hovering near record highs, does it make sense to max out your Roth IRA in 2021? Or should you hoard your cash in hopes of a sell-off? Here's why putting cash into your Roth IRA makes sense.

A person looks at a smartphone while balancing a laptop on a home couch.

Image source: Getty Images.

Why you should max out your Roth IRA

Roth IRAs have huge tax advantages. While you don't get an upfront tax break, your money grows tax-free. When you withdraw it, it's 100% tax-free and penalty-free, provided you're 59 1/2 and you've held the account for at least five years. 

Because the tax advantages are generous, Uncle Sam only lets us invest a limited amount: In 2021, the Roth IRA contribution limits are $6,000, or $7,000 if you're 50 or older.

Taking advantage of that tax-free growth is almost always a smart move. Don't worry too much about whether the stock market is up or down when you invest.

A Charles Schwab study found that a hypothetical investor lucky enough to invest $2,000 in the S&P 500 index at its lowest closing point each year between 2001 and 2020 would have had $151,391 at the end of 20 years. But if that investor had been unlucky enough to invest at the high point for each of those 20 years, their investment still would have grown to $121,171. Not too shabby on a $40,000 investment.

Of course, no one is lucky enough to consistently predict when the market will bottom out each year. Likewise, it would take an incredible streak of bad luck to always invest at the market's peak. The vast majority of investors will fall somewhere in between these two extremes.

One way to boost your odds of success as you invest your Roth IRA is to practice dollar-cost averaging, where you invest a predetermined amount on a set schedule. For example, you could contribute $500 a month, rather than investing $6,000 in a lump sum. Some months your money will go farther than others, but you'll reduce your risk of overpaying for your investments over time.

Who shouldn't fund a Roth IRA

There are a few circumstances where you shouldn't fund a Roth IRA. Most notably, if you don't have at least a three- to six-month emergency fund, focus on building that first. Doing so protects your investments in the event you need cash during a market slump.

If you have high-interest debt, like credit cards, pay it off first. The interest you're paying is probably higher than the average stock market returns.

Before you max out your Roth IRA, take advantage of any 401(k) employer match. Even if your company only matches 25% or 50%, that's essentially an automatic 25% or 50% return on your investment. Once you've gotten that free money, aim to contribute to your Roth IRA.

If you can afford to invest, every year is a good year to max out your Roth IRA. The compound earnings and tax savings will make your retirement years a whole lot richer.