Minecraft wiki entry for Improving_frame_rate, associated text: Use a high performance garbage collector such as ZGC or Shenandoah. These consume more CPU resources than the default one, but reduce lag spikes.
Allocate a reasonable amount of memory to Minecraft. Minecraft runs best with 2-4 GB of memory. Using ZGC or Shenandoah allows for large 16+ GB allocation without a performance penalty unlike the default G1GC collector. Be sure to leave memory for the system and other programs!
Use the latest OpenJDK to play Minecraft. If you're on Windows, install OpenJDK from Adoptium.