As the title suggests, I want to detect the exact timestamp when an element with a particular id or CSS class is inserted into the DOM (and possibly detect when it is removed). The most obvious method is to poll the document periodically, by calling
document.getElementById with the id of the element I want to find, but this method is not very accurate and can be very performance intensive if I have multiple elements I want to detect.
I took a look at Mutation Events and Mutation Observers, but from what I understand, in order to detect if an element is inserted or removed, I have to add a listener to the root DOM element, which can heavily impact performance if I need to execute additional JS each time an element is inserted or removed from the page.
Is there a more optimized solution that I am not aware of? Also, I would prefer if I didn't use an external JS library, even jQuery.