Both look bad on your report
From the credit rating point of view both are the same, and both are very bad to your credit. Otherwise, bankruptcy is preferable than foreclosure.
A real problem with foreclosure is that if your home does not sell for enough to pay off your mortgage, you are still responsible for the deficiency balance. That deficiency then becomes a lien and can be collected by garnishment of your wages, etc.
For people that are current on their loan then their lender may be interested in something called a deed in lieu of foreclosure. A deed is an agreement that the loan will be shown as paid in full (which is much better because their credit is not ruined) in exchange for them voluntarily deeding the property to the lender before a foreclosure is filed.
In your case however the lender won’t do a deed in lieu of foreclosure since your underwater on your loan.
Chapter 7 bankruptcy is the only way to get rid of any deficiency balance. In your case, since you own so much more than what the property is worth your best bet will be a bankruptcy.
So to be able to walk away without them coming after you for the balance – bankruptcy is your only option.
Good luck