Magento 2 – Different menu for Mobile theme

I have new custom theme that has default menu. I need to build mobile menu separately.

I can do it easily by media query (CSS) but I don’t think this is a good approach to hide desktop menu from mobile and hide mobile menu from desktop, since this is not a good approach if you focus on optimization.

Is it possible to check in PHP tha user is coming from mobile and do not show desktop menu, and wise versa for desktop menu.

Code would be something like this:

if(Mobile){     //Mobile menu code here } else{     //Desktop menu code here }