How to get memberId from a contactId

In my backend code, I have a list of a subset of my contacts, some of which are site members. I have the contactId for each. However, I also need the corresponding memberId for those contacts who have one, so that I can look up orders, etc. Is there an easy (or, failing that, complicated) way to find the corresponding member in my backend JS?

Well, I found a complicated way to do it. I used listMembers() to retrieve all members in the database, 100 at a time. For each of the members retrieved, I went through my entire list of contacts and compared the id of the contact with the contactId of the member. If they match, I set the memberId of that contact in my list.

Shockingly, I discovered that, at least in the instances I looked at, the memberId is the same as the contactId. So, my new question is:

Is the memberId ALWAYS the same as contactId, or are their circumstances in which they don’t match?

If they’re always the same, I am wasting my processing time getting this value.