I've always wondered this, too. It's almost always relieved within minutes of me taking the salt water. My concoction is salt + NoSalt in a shot glass of hot tap water, then a swig of pickle juice and/or apple cider vinegar... then a chaser of cold water, 'cause all that other stuff is gross. The pickle juice and vinegar may be folk remedies, but when your toes are curling themselves into shapes they shouldn't be able to at 2:00 am and you just want to get back to sleep, you'll try anything.
I think Nawchem is correct; especially when you're talking about middle-of-the-night cramps and your stomach is already empty. Anything you ingest will be absorbed pretty quickly. Imagine (or remember) consuming alcohol on an empty stomach. It might only take minutes to hit you.
I've thought of keeping some salt at the bedside, but I think getting up and walking off those cramps helps, too. Maybe because you're stimulating your circulation? My other rare last-ditch effort when the aforementioned doesn't work is to stand in the bath tub and run piping-hot water on my feet.
As for prevention, I try to be liberal with the salt and NoSalt, even though I'm not a big fan of salty foods. I also take calcium/magnesium with dinner. I was just taking magnesium, but I had a long spell during which I was still having night cramps, and it wasn't until I ran out of the Mg and started taking the Ca/Mg which I had bought by mistake
that they went away. Calcium is an important electrolyte, too.
Just to add to Gypsybird's question, I also wonder why whichever muscles cramp. Why not all of them? Why only one or two, and why not the same ones all the time? I swear, I've never had the exact same combination of weird foot muscle cramps.