i think the issue here is two fold.
1. Get-VsanObject -VM $vm
you are looking for the vsanobject associated with the supplied vm name, as no cluster is mentioned the search is performed at the vCenter level every time, which is time consuming.
2. Get-VM $vm
Again the same problem here.
i think using Get-View instead of using get-vm will reduce the time significantly. Will test the shared function and will post the results.