Skip to content

Commit

Permalink
Merge pull request #519 from gmetais/duplicatedQueriesFix
Browse files Browse the repository at this point in the history
Don't count duplicated queries on elements not attached to DOM
  • Loading branch information
Maciej Brencz committed May 25, 2015
2 parents d3faec1 + 426c5f9 commit 0adc54e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
10 changes: 9 additions & 1 deletion modules/domQueries/domQueries.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,15 @@ exports.module = function(phantomas) {
phantomas.log('DOM query: by %s - "%s" (using %s) in %s', type, query, fnName, context);
phantomas.incrMetric('DOMqueries');

DOMqueries.push(type + ' "' + query + '" (in ' + context + ')');
// Don't count document fragments or not yet inserted elements inside duplicated queries
if (context && (
context.indexOf('html') === 0 ||
context.indexOf('body') === 0 ||
context.indexOf('head') === 0 ||
context.indexOf('#document') === 0
)) {
DOMqueries.push(type + ' "' + query + '" (in ' + context + ')');
}
});

phantomas.on('report', function() {
Expand Down
16 changes: 8 additions & 8 deletions test/integration-spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
cssCount: 1
jsCount: 1
domains: 1
DOMqueries: 18
DOMqueries: 20
DOMqueriesById: 7
DOMqueriesByClassName: 1
DOMqueriesByTagName: 7
DOMqueriesByTagName: 9
DOMqueriesByQuerySelectorAll: 3
DOMinserts: 2
DOMmutationsInserts: 0
DOMqueriesDuplicated: 3
DOMqueriesAvoidable: 4
DOMqueriesDuplicated: 2
DOMqueriesAvoidable: 3
DOMqueriesWithoutResults: 4
# DOM operations (in SlimerJS)
- url: "/dom-operations.html"
Expand All @@ -43,15 +43,15 @@
cssCount: 1
jsCount: 1
domains: 1
DOMqueries: 18
DOMqueries: 20
DOMqueriesById: 7
DOMqueriesByClassName: 1
DOMqueriesByTagName: 7
DOMqueriesByTagName: 9
DOMqueriesByQuerySelectorAll: 3
DOMinserts: 2 # "div" appended to "html" (from jQuery) and the one below
DOMmutationsInserts: 1 # DocumentFragment > b[0]" appended to "body > p#foo"
DOMqueriesDuplicated: 3
DOMqueriesAvoidable: 4
DOMqueriesDuplicated: 2
DOMqueriesAvoidable: 3
DOMqueriesWithoutResults: 4
# DOM complexity
- url: "/dom-complexity.html"
Expand Down
5 changes: 5 additions & 0 deletions test/webroot/dom-operations.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
var nodeToRemove = document.getElementById('delete');
document.body.removeChild(nodeToRemove);

var detachedDiv1 = document.createElement('div');
var detachedDiv2 = document.createElement('div');

var nodes = [
document.querySelector('#foo'),
document.querySelector('ol li'), // DOMqueriesWithoutResults++
Expand All @@ -42,6 +45,8 @@
document.getElementById('list1').getElementsByTagName('li'),
document.getElementById('list2').getElementsByTagName('li'),
document.getElementById('foobar'), // DOMqueriesWithoutResults++
detachedDiv1.getElementsByTagName('a'),
detachedDiv2.getElementsByTagName('a'),// should not be counted as duplicated query
];

console.log(nodes);
Expand Down

0 comments on commit 0adc54e

Please sign in to comment.