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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
new-audit(unminified-javascript): detect savings from minifcation #3950
Changes from 1 commit
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,87 @@ | ||
/** | ||
* @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 ByteEfficiencyAudit = require('./byte-efficiency-audit'); | ||
const esprima = require('esprima'); | ||
|
||
const IGNORE_THRESHOLD_IN_PERCENT = .1; | ||
const IGNORE_THRESHOLD_IN_BYTES = 2048; | ||
|
||
class UnminifiedJavaScript extends ByteEfficiencyAudit { | ||
/** | ||
* @return {!AuditMeta} | ||
*/ | ||
static get meta() { | ||
return { | ||
name: 'unminified-javascript', | ||
description: 'Unminified JavaScript', | ||
informative: true, | ||
helpText: 'Minify JavaScript to save network bytes.', | ||
requiredArtifacts: ['Scripts', 'devtoolsLogs'], | ||
}; | ||
} | ||
|
||
/** | ||
* @param {string} scriptContent | ||
* @return {{minifiedLength: number, contentLength: number}} | ||
*/ | ||
static computeWaste(scriptContent, networkRecord) { | ||
const contentLength = scriptContent.length; | ||
let tokenLength = 0; | ||
let tokenLengthWithMangling = 0; | ||
|
||
const tokens = esprima.tokenize(scriptContent); | ||
for (const token of tokens) { | ||
tokenLength += token.value.length; | ||
// assume all identifiers could be reduced to a single character | ||
tokenLengthWithMangling += token.type === 'Identifier' ? 1 : token.value.length; | ||
} | ||
|
||
if (1 - tokenLength / contentLength < IGNORE_THRESHOLD_IN_PERCENT) return null; | ||
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. let's add a comment to indicate this is for handling pre-minified code. \o/ |
||
|
||
const totalBytes = ByteEfficiencyAudit.estimateTransferSize(networkRecord, contentLength, | ||
'script'); | ||
const wastedRatio = 1 - (tokenLength + tokenLengthWithMangling) / (2 * contentLength); | ||
const wastedBytes = Math.round(totalBytes * wastedRatio); | ||
|
||
return { | ||
url: networkRecord.url, | ||
totalBytes, | ||
wastedBytes, | ||
wastedPercent: 100 * wastedRatio, | ||
}; | ||
} | ||
|
||
/** | ||
* @param {!Artifacts} artifacts | ||
* @return {!Audit.HeadingsResult} | ||
*/ | ||
static audit_(artifacts, networkRecords) { | ||
const scriptsByUrl = artifacts.Scripts; | ||
|
||
const results = []; | ||
for (const [url, scriptContent] of scriptsByUrl.entries()) { | ||
const networkRecord = networkRecords.find(record => record.url === url); | ||
if (!networkRecord || !scriptContent) continue; | ||
|
||
const result = UnminifiedJavaScript.computeWaste(scriptContent, networkRecord); | ||
if (!result || result.wastedBytes < IGNORE_THRESHOLD_IN_BYTES) continue; | ||
results.push(result); | ||
} | ||
|
||
return { | ||
results, | ||
headings: [ | ||
{key: 'url', itemType: 'url', text: 'URL'}, | ||
{key: 'totalKb', itemType: 'text', text: 'Original'}, | ||
{key: 'potentialSavings', itemType: 'text', text: 'Potential Savings'}, | ||
], | ||
}; | ||
} | ||
} | ||
|
||
module.exports = UnminifiedJavaScript; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* @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 Gatherer = require('./gatherer'); | ||
const WebInspector = require('../../lib/web-inspector'); | ||
|
||
/** | ||
* @fileoverview Gets JavaScript content | ||
*/ | ||
class Scripts extends Gatherer { | ||
/** | ||
* @param {{driver: !Driver}} options | ||
* @param {{networkRecords: !Array<WebInspector.NetworkRequest}} traceData | ||
* @return {!Promise<!Map<string, string>>} | ||
*/ | ||
afterPass(options, traceData) { | ||
const driver = options.driver; | ||
|
||
const scriptContentMap = new Map(); | ||
const scriptRecords = traceData.networkRecords | ||
.filter(record => record.resourceType() === WebInspector.resourceTypes.Script) | ||
|
||
return scriptRecords.reduce((promise, record) => { | ||
return promise | ||
.then(() => { | ||
return driver.getRequestContent(record.requestId) | ||
.catch(err => null) | ||
.then(content => { | ||
if (!content) return; | ||
scriptContentMap.set(record.url, content); | ||
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. since we're definitely dealing with networkRecords i'd rather be using requestIds here as the key. over in the audit we could could then use i suppose this'll make testing slightly harder, so curious what you think. 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. yeah that's fair, if same URL was requested multiple times with different content we should surface that 馃憤 |
||
}); | ||
}) | ||
.then(() => scriptContentMap); | ||
}, Promise.resolve(scriptContentMap)); | ||
} | ||
} | ||
|
||
module.exports = Scripts; |
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.
can you add a comment here explaining the basic approach? using the description from this PR works for me.
let's also call out that inline scripts are not evaluated. (i dont think it matters in practice, but since other audits of ours include inline scripts we should just be explicit)
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.
yeah sounds good 馃憤
done