How can I check if an ID belongs to a user or a group?

I’ve got a SharePoint list with a field where authorized users and/or groups are listed. I know I can use CanCurrentUserViewMembership to check if the user belongs to a specific group, but if the ID I pass belongs to a user I get an Internal Server Error.

Is there a way to check whether the ID belongs to a person or group before making this call? Taking into consideration that I’m doing this on a loop, so a lot of calls are being made.