Speaking as an employer, the sooner you say the better .. although of course I can understand you wouldn't want to talk yourself out of a position until you were sure of having another!
Although it feels like a very personal thing to you, actually it's business! It's a contract for you do X in return for Y. Nothing more or less than that. So as long as you're not breaking any contract, you're free to do whatever you need to do.
The question is just how to do it as nicely and professionally as possible. So that is all the common sense stuff. Soon as you possibly can, speak to your employer. Explain your position. They may offer a compromise and you're free to accept or not of course :) failing that, if you can give them as much notice as possible that'll help them. Explain that you understand they'll be disappointed you're leaving so soon but you don't have a choice.
If they're in any way professional, they'll shake your hand, wish you all the best and thank you for giving them as much time to find a replacement as possible. If they get a bit nasty or snarky well then just remember that you should still stay professional even if they're not but don't get all wishy washy about it and make offers to do this or that which would be hard for you to keep.
End of the day, it's just business and if things were reversed and they had to let you go, they'd do it!