I noticed that on my site the Google Chrome developer tools “Performance Monitor” shows different numbers than those determined via getEventListeners()
and document.querySelectorAll('*')
.
As you can see in the screenshot below, the number of events calculated in the console is significantly higher than the one stated in the Chrome dev tools while the number of DOM Nodes is significantly lower.
Any idea how this can be explained?
function countDomEventListeners (elements) {
return Array.from(elements).reduce((count, node) => {
const listeners = getEventListeners(node)
for (var eventName in listeners) {
count += listeners[eventName].length
}
return count
}, 0)
}
elements = document.querySelectorAll('*')
console.log('DOM Nodes:', elements.length)
console.log('DOM event listeners:', countDomEventListeners(elements))