Skip to content

Commit

Permalink
core(unminified-javascript): exclude header size for estimating waste…
Browse files Browse the repository at this point in the history
…d bytes (#15670)
  • Loading branch information
connorjclark committed Dec 7, 2023
1 parent 8ab59ee commit 045c9bb
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
10 changes: 5 additions & 5 deletions cli/test/smokehouse/test-definitions/byte-efficiency.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,25 +187,25 @@ const expectations = {
items: [
{
url: 'http://localhost:10200/byte-efficiency/script.js',
wastedBytes: '46039 +/- 100',
wastedBytes: '45816 +/- 100',
wastedPercent: '87 +/- 5',
},
{
// /some-custom-url.js,
url: 'inline: \n function unusedFunction() {\n // U…',
wastedBytes: '6690 +/- 100',
wastedBytes: '6630 +/- 100',
wastedPercent: '99.6 +/- 0.1',
},
{
url: 'inline: \n // Used block #1\n // FILLER DATA JU…',
wastedBytes: '6569 +/- 100',
wastedBytes: '6510 +/- 100',
wastedPercent: 100,
},
{
url: 'http://localhost:10200/byte-efficiency/bundle.js',
totalBytes: '12962 +/- 1000',
wastedBytes: '2349 +/- 100',
wastedPercent: '19 +/- 5',
wastedBytes: '2303 +/- 100',
wastedPercent: '18 +/- 5',
},
],
},
Expand Down
4 changes: 2 additions & 2 deletions core/audits/byte-efficiency/unminified-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import {ByteEfficiencyAudit} from './byte-efficiency-audit.js';
import * as i18n from '../../lib/i18n/i18n.js';
import {computeJSTokenLength as computeTokenLength} from '../../lib/minification-estimator.js';
import {estimateTransferSize, getRequestForScript, isInline} from '../../lib/script-helpers.js';
import {estimateCompressedContentSize, getRequestForScript, isInline} from '../../lib/script-helpers.js';
import {Util} from '../../../shared/util.js';

const UIStrings = {
Expand Down Expand Up @@ -58,7 +58,7 @@ class UnminifiedJavaScript extends ByteEfficiencyAudit {
const contentLength = scriptContent.length;
const totalTokenLength = computeTokenLength(scriptContent);

const totalBytes = estimateTransferSize(networkRecord, contentLength, 'Script');
const totalBytes = estimateCompressedContentSize(networkRecord, contentLength, 'Script');
const wastedRatio = 1 - totalTokenLength / contentLength;
const wastedBytes = Math.round(totalBytes * wastedRatio);

Expand Down
10 changes: 6 additions & 4 deletions core/test/audits/byte-efficiency/unminified-javascript-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const KB = 1024;
const resourceType = 'Script';
describe('Page uses optimized responses', () => {
it('fails when given unminified scripts', () => {
const responseHeaders = [{name: 'Content-Encoding'}];
const commonRecord = {resourceType, responseHeaders};
const auditResult = UnminifiedJavascriptAudit.audit_({
URL: {finalDisplayedUrl: 'https://www.example.com'},
Scripts: [
Expand Down Expand Up @@ -59,10 +61,10 @@ describe('Page uses optimized responses', () => {
},
].map(createScript),
}, [
{requestId: '123.1', url: 'foo.js', transferSize: 20 * KB, resourceType},
{requestId: '123.2', url: 'other.js', transferSize: 50 * KB, resourceType},
{requestId: '123.3', url: 'valid-ish.js', transferSize: 100 * KB, resourceType},
{requestId: '123.4', url: 'invalid.js', transferSize: 100 * KB, resourceType},
{requestId: '123.1', url: 'foo.js', transferSize: 20 * KB, ...commonRecord},
{requestId: '123.2', url: 'other.js', transferSize: 50 * KB, ...commonRecord},
{requestId: '123.3', url: 'valid-ish.js', transferSize: 100 * KB, ...commonRecord},
{requestId: '123.4', url: 'invalid.js', transferSize: 100 * KB, ...commonRecord},
]);

const results = auditResult.items.map(item => Object.assign(item, {
Expand Down
10 changes: 5 additions & 5 deletions core/test/results/sample_v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -4453,7 +4453,7 @@
"scoreDisplayMode": "metricSavings",
"numericValue": 1200,
"numericUnit": "millisecond",
"displayValue": "Potential savings of 82 KiB",
"displayValue": "Potential savings of 81 KiB",
"warnings": [],
"metricSavings": {
"FCP": 0,
Expand Down Expand Up @@ -4481,13 +4481,13 @@
"items": [
{
"url": "http://localhost:10200/dobetterweb/third_party/aggressive-promise-polyfill.js",
"totalBytes": 166550,
"wastedBytes": 83568,
"totalBytes": 166320,
"wastedBytes": 83453,
"wastedPercent": 50.176166426166425
}
],
"overallSavingsMs": 1200,
"overallSavingsBytes": 83568,
"overallSavingsBytes": 83453,
"sortedBy": [
"wastedBytes"
],
Expand Down Expand Up @@ -10443,7 +10443,7 @@
"core/lib/i18n/i18n.js | displayValueByteSavings": [
{
"values": {
"wastedBytes": 83568
"wastedBytes": 83453
},
"path": "audits[unminified-javascript].displayValue"
},
Expand Down

0 comments on commit 045c9bb

Please sign in to comment.