note that a distance test is very expensive.
So expensive that when you are dealing with thousands of things in loops, you must weed out items that are not candidates.
You can do that with bounding boxes if closest means in any direction.
If you mean closest "perpendicular offset" like civils do, you must do the expensive test on every one, no short cuts.