From 30af255fd7e13da0417e68a7d71232bdfbdb549e Mon Sep 17 00:00:00 2001 From: gorhill Date: Wed, 24 Sep 2014 17:41:05 -0400 Subject: [PATCH] reduce overhead of mutation observer handler --- js/contentscript-end.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/js/contentscript-end.js b/js/contentscript-end.js index 000f505c7..1fd0edde8 100644 --- a/js/contentscript-end.js +++ b/js/contentscript-end.js @@ -542,7 +542,7 @@ var uBlockMessaging = (function(name){ iNode = nodeList.length; while ( iNode-- ) { node = nodeList[iNode]; - if ( typeof node.querySelectorAll !== 'function' ) { + if ( node.nodeType !== 1 ) { continue; } if ( ignoreTags.hasOwnProperty(node.tagName) ) { @@ -561,11 +561,12 @@ var uBlockMessaging = (function(name){ // https://github.com/gorhill/uBlock/issues/205 // Do not handle added node directly from within mutation observer. - var mutationObservedHandlerAsync = function(mutations) { + var treeMutationObservedHandlerAsync = function(mutations) { var iMutation = mutations.length; var nodeList; while ( iMutation-- ) { - if ( nodeList = mutations[iMutation].addedNodes ) { + nodeList = mutations[iMutation].addedNodes; + if ( nodeList.length !== 0 ) { addedNodeLists.push(nodeList); } } @@ -573,16 +574,14 @@ var uBlockMessaging = (function(name){ // I arbitrarily chose 50 ms for now: // I have to compromise between the overhead of processing too few // nodes too often and the delay of many nodes less often. - addedNodeListsTimer = setTimeout(mutationObservedHandler, 50); + addedNodeListsTimer = setTimeout(mutationObservedHandler, 75); } }; // https://github.com/gorhill/httpswitchboard/issues/176 - var observer = new MutationObserver(mutationObservedHandlerAsync); - observer.observe(document.body, { - attributes: false, + var treeObserver = new MutationObserver(treeMutationObservedHandlerAsync); + treeObserver.observe(document.body, { childList: true, - characterData: false, subtree: true }); })();