I’ve realized that the typing lag happens because of the aggressive frequency decreases on iGPU, it goes down to 0 MHz. You can observe this with an Intel power gadget, or iStat Menus. For instance, when you play a video on YouTube, there’s no lag while typing, because the iGPU stays around 100-150 MHz. The typing lag starts again when you switch to another application or another tab, because iGPU goes to 0 MHz again, even though the video is playing in the background. Also, there is no way to control the CPU and GPU frequencies on macOS. So I was thinking about what would keep the iGPU active with optimal minimum frequency and not cause the typing lag. Finally, I’ve figured it out, play a YouTube video on Safari, put it in Picture in Picture Mode, then minimize it and hide it in some corners. macOS doesn’t pause the video, and somehow iGPU stays between 35-100 MHz, which prevents the typing lag. Total power consumption is less than using a discrete GPU all the time. It works with or without low power mode.
The video I play:
8 hours youtube video
How to enable Picture in Picture:
https://www.macrumors.com/how-to/picture-in-picture-video-mac/
After putting the video pip mode, just make the box smaller and hide it somewhere, I prefer to put it next to the trash bin, like below.