reduce overhead of mutation observer handler

This commit is contained in:
gorhill 2014-09-24 17:41:05 -04:00
parent 17838883da
commit 30af255fd7

View file

@ -542,7 +542,7 @@ var uBlockMessaging = (function(name){
iNode = nodeList.length; iNode = nodeList.length;
while ( iNode-- ) { while ( iNode-- ) {
node = nodeList[iNode]; node = nodeList[iNode];
if ( typeof node.querySelectorAll !== 'function' ) { if ( node.nodeType !== 1 ) {
continue; continue;
} }
if ( ignoreTags.hasOwnProperty(node.tagName) ) { if ( ignoreTags.hasOwnProperty(node.tagName) ) {
@ -561,11 +561,12 @@ var uBlockMessaging = (function(name){
// https://github.com/gorhill/uBlock/issues/205 // https://github.com/gorhill/uBlock/issues/205
// Do not handle added node directly from within mutation observer. // Do not handle added node directly from within mutation observer.
var mutationObservedHandlerAsync = function(mutations) { var treeMutationObservedHandlerAsync = function(mutations) {
var iMutation = mutations.length; var iMutation = mutations.length;
var nodeList; var nodeList;
while ( iMutation-- ) { while ( iMutation-- ) {
if ( nodeList = mutations[iMutation].addedNodes ) { nodeList = mutations[iMutation].addedNodes;
if ( nodeList.length !== 0 ) {
addedNodeLists.push(nodeList); addedNodeLists.push(nodeList);
} }
} }
@ -573,16 +574,14 @@ var uBlockMessaging = (function(name){
// I arbitrarily chose 50 ms for now: // I arbitrarily chose 50 ms for now:
// I have to compromise between the overhead of processing too few // I have to compromise between the overhead of processing too few
// nodes too often and the delay of many nodes less often. // 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 // https://github.com/gorhill/httpswitchboard/issues/176
var observer = new MutationObserver(mutationObservedHandlerAsync); var treeObserver = new MutationObserver(treeMutationObservedHandlerAsync);
observer.observe(document.body, { treeObserver.observe(document.body, {
attributes: false,
childList: true, childList: true,
characterData: false,
subtree: true subtree: true
}); });
})(); })();