Skip to content

Commit

Permalink
join classlist instead of using toString -> removes insignificant whi…
Browse files Browse the repository at this point in the history
…tespace, do not create hashes for empty class attributes.
  • Loading branch information
fbuchinger committed Mar 23, 2017
1 parent 9c01d04 commit 581f2e3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/layoutstats.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,12 @@ LayoutStats.addMetric({
}
return hash >>> 0;
}
// return css classlist as a hash to decrease the size of the JSON metric
return {key: hash(node.classList.toString().trim()).toString(36), value: 1};

var cssClassList = Array.prototype.join.call(node.classList," ");
// return css classlist as a hash to decrease the size of the JSON payload
if (cssClassList.length > 0) {
return {key: hash(cssClassList).toString(36), value: 1};
}
},
reduce: ['uniquekeylist']
});
Expand Down
4 changes: 4 additions & 0 deletions test/spec/layoutstats.nodemetrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
QUnit.test("measures the css class attributes used by child nodes. It ", function (assert) {
var ls = injectAndGetLayoutStats('<div class="foo1 foo2 foo3"></div><span class="foo1 foo2 foo3"></span><div class="bar1 bar2 bar2"></div>');
assert.deepEqual(ls.nodeUsedCSSClassAttributesList.sort(),["1bd623p", "c8zwxf"].sort(),"creates an array of unique hashed class list combinations");
var ls = injectAndGetLayoutStats('<div class="foo1 foo2 foo3"></div><span class="foo1 foo2 foo3"></span>');
assert.deepEqual(ls.nodeUsedCSSClassAttributesList.sort(),["c8zwxf"],"ignores insignificant whitespace between the classnames when building the hash");
var ls = injectAndGetLayoutStats('<div class=" "></div><span class=""></span>');
assert.deepEqual(ls.nodeUsedCSSClassAttributesList.sort(),[],"ignores empty class attributes");
});

})( window, QUnit );

0 comments on commit 581f2e3

Please sign in to comment.