I believe you have also opened a JIRA ticket about this: SERVER-48349. We should keep the discussion in that ticket so all information is available in a single place.
One thing I would note is that although you’re talking about the running time of the compact command, I don’t believe you mentioned whether any reduction in file size was actually visible in both 3.4.0 and 3.6.17. It might be worth putting that detail into the SERVER ticket.
Note that the 3.4 series is out of support since January 2020, and so it’s possible that you’re seeing the effect of an old bug or similar.