reduce overhead of mutation observer handler

This commit is contained in:
gorhill 2014-09-24 17:41:05 -04:00
parent 17838883da
commit 30af255fd7
1 changed files with 7 additions and 8 deletions

View File

@ -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
});
})();