The other answers are good but don't really address your question about time:
Is it only equal if you intend to keep the money in the stock market for exactly the same amount of time as you would be paying off the loan?
I suggest thinking of where to put your money not as a one-time decision but as something that will be reevaluated periodically, say once a year. There are complicating factors, but all else being equal, more money (higher return) now is better than less. Later, you can make a new decision based on circumstances at the time, but you will reach that point with more money if you maximize your return this year.
The rough analogy is that you are putting your money to work. If you can invest at X% return, then each of your dollars earns a "wage" of X cents per year. Importantly, a dollar can only earn one investment "wage" at a time, though of course you can split your dollars between investments or move them back and forth (just as you personally can have two jobs, but there are only so many hours in the year and you generally can't spend the same hour working for company A and company B).
However long your money remains in a given investment, when you take it out you will be looking for the best new investment at that time (just as when you leave a job you will typically have, or find, another job). But you can never get back the time that has elapsed. Since time is the ultimate scarce resource, it makes sense that returns are compared as rates (per unit time) just as wages are.