All three military exchanges are non-appropriated fund activity (NAF) of the Department of Defense, the exchange services fund 98% of their operating budgets (civilian employee salaries, inventory investments, utilities and capital investments for equipment, vehicles and facilities) from the sale of merchandise, food and services to customers.
This means almost all of their bills (to include merchandise buying) are paid ‘at the register’. Since there is very little house brand items that could drive higher income margins, the exchanges have to charge at least cost + cost of doing business.
For some items (liquor and gas, primarily) the exchanges survey the local market and attempt to be just below the market prices.
Places like Walmart and other discount/low cost chains will always beat the exchanges on price.
Where you find the savings is generally on name brand clothing.
The firearms are purchased through major distributors and must adhere to distributor pricing policies as well as help pay the bills at the register.
Just to add that exchanges incorporate any "profits" they make back into the military community/installation.
Additionally, given their "price matching guarantee," if you download the Gun Deals app to your smart phone, and get the lowest price for a weapon being sold at the exchange, they will match that price. You not only end up getting the best price, but no taxes, shipping or FFL fees!
Lastly, from experience I have never seen on-base gas prices any cheaper than off-base.