-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
script-helpers.js
167 lines (155 loc) · 6.86 KB
/
script-helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**
* @license
* Copyright 2022 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {NetworkRequest} from './network-request.js';
/**
* @param {LH.Artifacts.Script} script
* @return {boolean}
*/
function isInline(script) {
return Boolean(script.startLine || script.startColumn);
}
/**
* @param {LH.Artifacts.NetworkRequest[]} networkRecords
* @param {LH.Artifacts.Script} script
* @return {LH.Artifacts.NetworkRequest|undefined}
*/
function getRequestForScript(networkRecords, script) {
let networkRequest = networkRecords.find(request => request.url === script.url);
while (networkRequest?.redirectDestination) {
networkRequest = networkRequest.redirectDestination;
}
return networkRequest;
}
/**
* Estimates the number of bytes this network record would have consumed on the network based on the
* uncompressed size (totalBytes). Uses the actual transfer size from the network record if applicable.
*
* @param {LH.Artifacts.NetworkRequest|undefined} networkRecord
* @param {number} totalBytes Uncompressed size of the resource
* @param {LH.Crdp.Network.ResourceType=} resourceType
* @return {number}
*/
function estimateTransferSize(networkRecord, totalBytes, resourceType) {
if (!networkRecord) {
// We don't know how many bytes this asset used on the network, but we can guess it was
// roughly the size of the content gzipped.
// See https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/optimize-encoding-and-transfer for specific CSS/Script examples
// See https://discuss.httparchive.org/t/file-size-and-compression-savings/145 for fallback multipliers
switch (resourceType) {
case 'Stylesheet':
// Stylesheets tend to compress extremely well.
return Math.round(totalBytes * 0.2);
case 'Script':
case 'Document':
// Scripts and HTML compress fairly well too.
return Math.round(totalBytes * 0.33);
default:
// Otherwise we'll just fallback to the average savings in HTTPArchive
return Math.round(totalBytes * 0.5);
}
} else if (networkRecord.resourceType === resourceType) {
// This was a regular standalone asset, just use the transfer size.
return networkRecord.transferSize || 0;
} else {
// This was an asset that was inlined in a different resource type (e.g. HTML document).
// Use the compression ratio of the resource to estimate the total transferred bytes.
const transferSize = networkRecord.transferSize || 0;
const resourceSize = networkRecord.resourceSize || 0;
// Get the compression ratio, if it's an invalid number, assume no compression.
const compressionRatio = Number.isFinite(resourceSize) && resourceSize > 0 ?
(transferSize / resourceSize) : 1;
return Math.round(totalBytes * compressionRatio);
}
}
/**
* Estimates the number of bytes the content of this network record would have consumed on the network based on the
* uncompressed size (totalBytes). Uses the actual transfer size from the network record if applicable,
* minus the size of the response headers.
*
* This differs from `estimateTransferSize` only in that is subtracts the response headers from the estimate.
*
* @param {LH.Artifacts.NetworkRequest|undefined} networkRecord
* @param {number} totalBytes Uncompressed size of the resource
* @param {LH.Crdp.Network.ResourceType=} resourceType
* @return {number}
*/
function estimateCompressedContentSize(networkRecord, totalBytes, resourceType) {
if (!networkRecord) {
// We don't know how many bytes this asset used on the network, but we can guess it was
// roughly the size of the content gzipped.
// See https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/optimize-encoding-and-transfer for specific CSS/Script examples
// See https://discuss.httparchive.org/t/file-size-and-compression-savings/145 for fallback multipliers
switch (resourceType) {
case 'Stylesheet':
// Stylesheets tend to compress extremely well.
return Math.round(totalBytes * 0.2);
case 'Script':
case 'Document':
// Scripts and HTML compress fairly well too.
return Math.round(totalBytes * 0.33);
default:
// Otherwise we'll just fallback to the average savings in HTTPArchive
return Math.round(totalBytes * 0.5);
}
}
// Get the size of the response body on the network.
let contentTransferSize = networkRecord.transferSize || 0;
if (!NetworkRequest.isContentEncoded(networkRecord)) {
// This is not encoded, so we can use resourceSize directly.
// This would be equivalent to transfer size minus headers transfer size, but transfer size
// may also include bytes for SSL connection etc.
contentTransferSize = networkRecord.resourceSize;
} else if (networkRecord.responseHeadersTransferSize) {
// Subtract the size of the encoded headers.
contentTransferSize =
Math.max(0, contentTransferSize - networkRecord.responseHeadersTransferSize);
}
if (networkRecord.resourceType === resourceType) {
// This was a regular standalone asset, just use the transfer size.
return contentTransferSize;
} else {
// This was an asset that was inlined in a different resource type (e.g. HTML document).
// Use the compression ratio of the resource to estimate the total transferred bytes.
const resourceSize = networkRecord.resourceSize || 0;
// Get the compression ratio, if it's an invalid number, assume no compression.
const compressionRatio = Number.isFinite(resourceSize) && resourceSize > 0 ?
(contentTransferSize / resourceSize) : 1;
return Math.round(totalBytes * compressionRatio);
}
}
/**
* Utility function to estimate the ratio of the compression on the resource.
* This excludes the size of the response headers.
* Also caches the calculation.
* @param {Map<string, number>} compressionRatioByUrl
* @param {string} url
* @param {LH.Artifacts} artifacts
* @param {Array<LH.Artifacts.NetworkRequest>} networkRecords
*/
function estimateCompressionRatioForContent(compressionRatioByUrl, url,
artifacts, networkRecords) {
let compressionRatio = compressionRatioByUrl.get(url);
if (compressionRatio !== undefined) return compressionRatio;
const script = artifacts.Scripts.find(script => script.url === url);
if (!script) {
// Can't find content, so just use 1.
compressionRatio = 1;
} else {
const networkRecord = getRequestForScript(networkRecords, script);
const contentLength = networkRecord?.resourceSize || script.length || 0;
const compressedSize = estimateCompressedContentSize(networkRecord, contentLength, 'Script');
compressionRatio = compressedSize / contentLength;
}
compressionRatioByUrl.set(url, compressionRatio);
return compressionRatio;
}
export {
getRequestForScript,
isInline,
estimateCompressedContentSize,
estimateTransferSize,
estimateCompressionRatioForContent,
};