This depends on what kind of relationship you have with your boss at your current company. If you have a generally cooperative and honest relationship, then be nice and give them the most possible warning.
Don't just say "I'm outta here in 4 weeks.". Something in a more cooperative spirit would be better: "I'm leaving in 4 weeks. I'm telling you this now because Acme Company and you have always been fair to me, and I want to give you the most time to plan and figure out how I can best use my remaining time to make this as smooth as possible for you.". Unless the boss really can't stand you and was looking for a opportunity to dump you, you're not going to get terminated early.
Don't be surprised if a lot of your remaining time is spent making sure whatever you worked on is properly documented, and your current project is transitioned to someone else. That's just how this works, and should not be taken as a insult or lack of confidence in your abilities.
The worst the existing company can do is lay you off on the spot, in which case you'll still get paid for 2 of the 4 weeks at minimum, unless you have a very unusual employment contract. However, the upside is more substantial. You may not need anything from your current company now, but it's always good in the long term to leave on good terms. Next job change, being able to use your current boss as a reference could be quite valuable. It's never good to burn bridges behind you. In fact, it's worth some effort and some risk to try to do the opposite. By giving them early notice, you do them a favor on the way out. Unless your boss is a complete jerk, that won't be forgotten.