I misread the question. (sorry! edited my post.)
As far as lookup time goes, DNS lookup for “localhost” happens on your local machine (that’s what the
hosts file is for), so it will actually be much quicker than DNS lookup for any address you visit on the web. Yes, if you use the IP address you don’t need to do a lookup at all, but you will never notice the difference in speed.
Using the IP address for your DB connection actually has more to do with the possibility that “localhost” won’t successfully find your DB at all (so, yes, do it). “localhost” will either work or it won’t; it’s not a speed issue.