This summer I used a loan from my 401(k) to help pay for the down payment of a new house. We planned on selling a Condo a few months later, so we only needed the loan for a short period but wanted to keep monthly payments low since we would be paying two mortgages for a few months. I also felt like the market might take a dip in the future, so I liked the idea of trying to cash out high and buy back low (spoiler alert: this didn't happen).
So in July 2017 I withdrew $17,000 from my account (Technically $16,850.00 principal and $150 processing fee) at an effective 4.19% APR (4% rate and then the fee), with 240 scheduled payments of $86.00 (2 per month for 10 years). Over the lifetime of the loan the total finance charge was $3,790, but that money would be paid back into my account. I was happy with the terms, and it helped tide things over until the condo was sold a few months later. But then I decided to change jobs, and ended up having to pay back the loan ~20 weeks after it was issued (using the proceeds from the sale of the condo).
During this time the market had done well, so when I paid back the funds the net difference in shares that I now owned (including shares purchased with the interest payments) was $538.25 less than today's value of the original count of shares that were sold to fund the loan. Combined with the $150 fee, the overall "cost" of the 20 week loan was about 4.05%. That isn't the interest rate (interest was paid back to my account balance), but the value lost due to the principal having been withdrawn. On paper, my account would be worth that much more if I hadn't withdrawn the money.
Now if you extrapolate the current market return into 52 weeks, you can think of that loan having an APR "cost" of around 10.5% (Probably not valid for a multi year calculation, but seems accurate for a 12 month projection). Again, that is not interest paid back to the account, but instead the value lost due to the money not being in the account. Sure, the market could take a dip and I may be able to buy the shares back at a reduced cost, but that would require keeping sizable liquid assets around and trying to time the market. It also is not something you can really schedule very well, as the loan took 6 days to fund (not including another week of clarifying questions back/forth before that) and 10 day to repay (from the time I initiated the paperwork to when the check was cashed and shares repurchased).
So in my experience, the true cost of the loan greatly depends on how the market does, and if you have the ability to pay back the loan it probably is worth doing so. Especially since you may be forced to do so at any time if you change jobs or your employment is terminated.