The principal difference is your code executes a query, wich if the $ids is empty the query with the argument “post__in” will be empty, so retrieves all of the testimonials posts, and then checks if you have $ids or not. So this does a call to the database and then checks for the $ids.
In my code checks if you have $ids or not, if you don’t have $ids then don’t do the query (which means don’t do a call to the database)
I hope this helps you.