The answer to which is "better in the long term" depends on your goals.
The HSA is, in a way, the best investment because money that is contributed is tax-free and the money taken out for qualified medical expenses is also tax-free (including any earnings).
Compare this to a traditional IRA or 401(k) plan, where the contributions are tax-free when made, but the distributions are taxed when withdrawn (including the earnings).
Compare this to a Roth IRA or 401(k) plan where the contributions are taxed when made, but the distributions are not taxed (including the earnings).
So, looked from this point of view, the ranking in descending order of financial effectiveness would be:
- HSA
- Roth IRA or 401(k)
- Traditional IRA or 401(k)
However, this is not the whole story.
If you have an emergency and need to withdraw money from one of these accounts, the penalties vary:
- From an HSA, a distribution for something other than a qualified medical expenses will not only be subject to regular federal income tax, but also a 20% penalty in addition.
- From a traditional IRA and 401(k), an "early" withdrawal is taxed as regular income AND pays a 10% penalty - but the 10% penalty can be waived in a few cases.
- From a Roth IRA or 401(k) plan, an "early" withdrawal taxed only to the extent that the distribution excess the contributions, and the 10% penalty may also apply.
Viewed this way, the descending order of "best" from a financial point of view is
- Roth IRA or 401(k)
- Traditional IRA or 401(k)
- HSA
And note that a 401(k) plan (but not an HSA or an IRA) may allow the taxpayer to take a loan from the money in the plan. This means that after the taxpayer has repaid the emergency loan, the taxpayer is not out anything because the interest was also paid into the 401(k) plan.
And, of course, the investments that you can choose will vary greatly from HSA to IRA to 401(k).