The best weapon if you have to use a weapon, is going to be a firearm and specifically the one you are most familiar with. The caveat to that would be if you’re only familiar with shooting high powered rifles and you live in an apartment building you might want to seriously consider an alternative protection strategy for your home due to the huge risk to innocents.
Every situation is different. I once lived in an apartment above a bar, because it was on a hill the windows out of the bedrooms were literally a death drop to the street below. The front windows opened up to the walkway outside, but that was also the only realistic place someone would be coming in so if someone was breaking into that apartment with intent to do harm it really was a “go down fighting” scenario. There simply were no other options.
The reality is a “home invasion” in which a group of armed intruders “invades your home” is beyond rare. Usually they are part of organized criminal activity targeting specific people for specific reasons. Most “ordinary” home burglaries, a home alarm going off, a dog barking, or a homeowner yelling will end with the burglar running. (My one home burglary when I was young, basically me screaming and waving a stick at the guy on my front porch resulted in him booking it.)
But since this is a speculative thread, my main concern in a home invasion wouldn’t be specific weapon I’d use but instead my plan to come out of it alive. I’ve been trained in modern military arms and tactics, but the reality is anyone can be taken out by anyone in a gunfight. Since I’m living a quiet suburban life here in Virginia my last desire on a daily basis is to get in a gunfight against an unknown number of people with unknown weapons coming upon me in my sleep.
So in my house I have a good alarm system and a dog, and FWIW my house is well made and I have lots of heavy doors, my entrance doors aren’t easily or quickly breached and etc. Even my interior doors are solid core, so would be of some use. If the alarm went off my first move would be to push my very heavy, solid oak dresser in front of my bedroom door. I’m very confident I could do this before anyone got to that part of the house. At that point my bedroom is fairly secured (obviously if someone is just spraying bullets through the walls, I’m in trouble.) Right now I have no firearms in my bedroom, at one point I kept some in a smaller gun safe in my bedroom closet, assuming I still had that setup I’d get a shotgun out, get my phone and then go outside. My bedroom is not on ground level and actually has a sliding door that opens to a small deck/balcony that is only accessible from the master bedroom. I’d surreptitiously look around ground level to see if I saw anyone. If not, I’d consider trying to climb down the balcony to the yard and then fleeing to a neighbor’s. But the balcony because of the way it is made can’t really be climbed up or down and would instead basically requiring sliding down a wall or just jumping.
I’m confident the jump is survivable, but not confident I could do it without breaking bones. Most likely what I’d do is climb onto the roof of the house and slink around to a spot on the roof where I’d be protected by a chimney and would mostly know if anyone was coming. Then I would call 911 (I didn’t do this earlier because my alarm going off is supposed to do that for me, but at this point I’d call to make sure.) Depending on what I heard and what was going on, I’d consider making my way over to the garage roof and then climbing down to the yard (much easier there than from the bedroom), but I might just wait on the roof trusting armed intruders wouldn’t immediately even know I was up there or tell how to get there.