-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Audit: DOM stats (total nodes, depth, width) #1673
Changes from all commits
dbec36e
52f7034
dc798e5
c4b4370
3c367b8
1ec26e0
c037cb8
6531bd5
08346cf
22dc064
1d51d6e
1eef7bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<!doctype html> | ||
<!-- | ||
* Copyright 2017 Google Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
--> | ||
<html> | ||
<head> | ||
<title>DoBetterWeb - DOM size tester</title> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0"> | ||
</head> | ||
<body> | ||
<main> | ||
<section> | ||
<div> | ||
<div>6</div> | ||
</div> | ||
<div> | ||
<div> | ||
<div id="attachshadow">7</div> | ||
</div> | ||
</div> | ||
<div> | ||
<template> | ||
<div><div><div><div>10, but noop in template</div></div></div></div> | ||
</template> | ||
</div> | ||
</section> | ||
</main> | ||
<footer> | ||
<div id="attachshadow-big" class="test this out">4</div> | ||
<div>3</div> | ||
<div></div> | ||
<div></div> | ||
<div></div> | ||
<div></div> | ||
</footer> | ||
<script> | ||
function withShadowDOMTest() { | ||
const el = document.querySelector('#attachshadow'); | ||
el.innerHTML = `<div>7</div>`; | ||
el.attachShadow({mode: 'open'}).innerHTML = ` | ||
<div> | ||
<div class="here">9</div> | ||
</div> | ||
`; | ||
|
||
const el2 = document.querySelector('#attachshadow-big'); | ||
el2.attachShadow({mode: 'open'}).innerHTML = ` | ||
<div> | ||
<div>6</div> | ||
<div>6</div> | ||
<div>6</div> | ||
<div>6</div> | ||
<div>6</div> | ||
<div>6</div> | ||
<div>6</div> | ||
<div>6</div> | ||
<div>6</div> | ||
</div> | ||
`; | ||
} | ||
|
||
function domTest(numNodes=1500) { | ||
const frag = new DocumentFragment(); | ||
for (let i = 0; i < numNodes; ++i) { | ||
frag.appendChild(document.createElement('span')); | ||
} | ||
document.body.appendChild(frag); | ||
} | ||
|
||
const params = new URLSearchParams(location.search); | ||
if (params.has('smallDOM')) { | ||
domTest(1300); | ||
} else if (params.has('largeDOM')) { | ||
domTest(6000); | ||
} | ||
if (params.has('withShadowDOM')) { | ||
withShadowDOMTest(); | ||
} | ||
</script> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/** | ||
* @license | ||
* Copyright 2017 Google Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* @fileoverview Audits a page to see how the size of DOM it creates. Stats like | ||
* tree depth, # children, and total nodes are returned. The score is calculated | ||
* based solely on the total number of nodes found on the page. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const Audit = require('../audit'); | ||
const TracingProcessor = require('../../lib/traces/tracing-processor'); | ||
const Formatter = require('../../formatters/formatter'); | ||
|
||
const MAX_DOM_NODES = 1500; | ||
const MAX_DOM_TREE_WIDTH = 60; | ||
const MAX_DOM_TREE_DEPTH = 32; | ||
|
||
// Parameters for log-normal CDF scoring. See https://www.desmos.com/calculator/9cyxpm5qgp. | ||
const SCORING_POINT_OF_DIMINISHING_RETURNS = 2400; | ||
const SCORING_MEDIAN = 3000; | ||
|
||
class DOMSize extends Audit { | ||
static get MAX_DOM_NODES() { | ||
return MAX_DOM_NODES; | ||
} | ||
|
||
/** | ||
* @return {!AuditMeta} | ||
*/ | ||
static get meta() { | ||
return { | ||
category: 'Performance', | ||
name: 'dom-size', | ||
description: 'Avoids an excessive DOM size', | ||
optimalValue: DOMSize.MAX_DOM_NODES.toLocaleString() + ' nodes', | ||
helpText: 'Browser engineers recommend pages contain fewer than ' + | ||
`~${DOMSize.MAX_DOM_NODES.toLocaleString()} DOM nodes. The sweet spot is a tree depth < ` + | ||
`${MAX_DOM_TREE_DEPTH} elements and fewer than ${MAX_DOM_TREE_WIDTH} ` + | ||
'children/parent element. A large DOM can increase memory, cause longer ' + | ||
'[style calculations](https://developers.google.com/web/fundamentals/performance/rendering/reduce-the-scope-and-complexity-of-style-calculations), ' + | ||
'and produce costly [layout reflows](https://developers.google.com/speed/articles/reflow). [Learn more](https://developers.google.com/web/fundamentals/performance/rendering/).', | ||
requiredArtifacts: ['DOMStats'] | ||
}; | ||
} | ||
|
||
/** | ||
* @param {!Artifacts} artifacts | ||
* @return {!AuditResult} | ||
*/ | ||
static audit(artifacts) { | ||
const stats = artifacts.DOMStats; | ||
|
||
/** | ||
* html > | ||
* body > | ||
* div > | ||
* span | ||
*/ | ||
const depthSnippet = stats.depth.pathToElement.reduce((str, curr, i) => { | ||
return `${str}\n` + ' '.repeat(i) + `${curr} >`; | ||
}, '').replace(/>$/g, '').trim(); | ||
const widthSnippet = 'Element with most children:\n' + | ||
stats.width.pathToElement[stats.width.pathToElement.length - 1]; | ||
|
||
// Use the CDF of a log-normal distribution for scoring. | ||
// <= 1500: score≈100 | ||
// 3000: score=50 | ||
// >= 5970: score≈0 | ||
const distribution = TracingProcessor.getLogNormalDistribution( | ||
SCORING_MEDIAN, SCORING_POINT_OF_DIMINISHING_RETURNS); | ||
let score = 100 * distribution.computeComplementaryPercentile(stats.totalDOMNodes); | ||
|
||
// Clamp the score to 0 <= x <= 100. | ||
score = Math.max(0, Math.min(100, score)); | ||
|
||
const cards = [ | ||
{title: 'Total DOM Nodes', value: stats.totalDOMNodes.toLocaleString()}, | ||
{title: 'DOM Depth', value: stats.depth.max.toLocaleString(), snippet: depthSnippet}, | ||
{title: 'Maximum Children', value: stats.width.max.toLocaleString(), snippet: widthSnippet} | ||
]; | ||
|
||
return DOMSize.generateAuditResult({ | ||
rawValue: stats.totalDOMNodes, | ||
optimalValue: this.meta.optimalValue, | ||
score: Math.round(score), | ||
displayValue: `${stats.totalDOMNodes.toLocaleString()} nodes`, | ||
extendedInfo: { | ||
formatter: Formatter.SUPPORTED_FORMATS.CARD, | ||
value: cards | ||
} | ||
}); | ||
} | ||
|
||
} | ||
|
||
module.exports = DOMSize; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* @license | ||
* Copyright 2017 Google Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const Formatter = require('./formatter'); | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const html = fs.readFileSync(path.join(__dirname, 'partials/cards.html'), 'utf8'); | ||
|
||
class Card extends Formatter { | ||
static getFormatter(type) { | ||
switch (type) { | ||
case 'pretty': | ||
return result => { | ||
if (!result || !Array.isArray(result)) { | ||
return ''; | ||
} | ||
let output = ''; | ||
result.forEach(item => { | ||
output += ` - ${item.title}: ${item.value}\n`; | ||
}); | ||
return output; | ||
}; | ||
|
||
case 'html': | ||
// Returns a handlebars string to be used by the Report. | ||
return html; | ||
|
||
default: | ||
throw new Error('Unknown formatter type'); | ||
} | ||
} | ||
} | ||
|
||
module.exports = Card; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
.cards__container { | ||
--padding: 16px; | ||
display: flex; | ||
flex-wrap: wrap; | ||
} | ||
.scorecard { | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
flex: 1 1 150px; | ||
flex-direction: column; | ||
padding: calc(var(--padding) / 2); | ||
padding-top: calc(32px + var(--padding) / 2); | ||
border-radius: 3px; | ||
margin-right: var(--padding); | ||
position: relative; | ||
color: var(--secondary-text-color); | ||
line-height: inherit; | ||
border: 1px solid #ebebeb; | ||
} | ||
.scorecard-title { | ||
/*text-transform: uppercase;*/ | ||
font-size: var(--subitem-font-size); | ||
line-height: var(--heading-line-height); | ||
background-color: #eee; | ||
position: absolute; | ||
top: 0; | ||
right: 0; | ||
left: 0; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
border-bottom: 1px solid #ebebeb; | ||
} | ||
.scorecard-value { | ||
font-size: 24px; | ||
} | ||
.scorecard-summary { | ||
margin-top: calc(var(--padding) / 2); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<ul class="subitem__details cards__container"> | ||
{{#each this}} | ||
<div class="subitem__detail scorecard" {{#if snippet}}title="{{snippet}}"{{/if}}> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if it will always be the title can we rename snippet to title? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I reserved There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh whoops, alright |
||
<div class="scorecard-title">{{title}}</div> | ||
<div class="scorecard-value">{{value}}</div> | ||
</div> | ||
{{/each}} | ||
</ul> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yay for moving out into some new testers! 🎉