If your getting lag one of the worst things in my opinion is to factory reset your pc (learnt about it in computing), your best bet, it to check for all updates with security software, make sure you don't have anti-virus scans running in the background, and ensure that you don't have random tasks running in the background (like ones that are from companies you have never heard of, microsoft programs are perfectly normal). Also make sure you are using the latest version of optifine :-D When you go into task manager, tell me if it says that anything is on 100% usage, i can recommend some YT videos to fix that (most of the time they work).