-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
unused-javascript-summary.js
154 lines (133 loc) · 4.4 KB
/
unused-javascript-summary.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
/**
* @license Copyright 2020 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {makeComputedArtifact} from './computed-artifact.js';
/**
* @typedef WasteData
* @property {Uint8Array} unusedByIndex
* @property {number} unusedLength
* @property {number} contentLength
*/
/**
* @typedef ComputeInput
* @property {string} scriptId
* @property {Omit<LH.Crdp.Profiler.ScriptCoverage, 'url'>} scriptCoverage
* @property {LH.Artifacts.Bundle=} bundle
*/
/**
* @typedef Summary
* @property {string} scriptId
* @property {number} wastedBytes
* @property {number} totalBytes
* @property {number} wastedBytes
* @property {number=} wastedPercent
* @property {Record<string, number>=} sourcesWastedBytes Keyed by file name. Includes (unmapped) key too.
*/
class UnusedJavascriptSummary {
/**
* @param {Omit<LH.Crdp.Profiler.ScriptCoverage, 'url'>} scriptCoverage
* @return {WasteData}
*/
static computeWaste(scriptCoverage) {
let maximumEndOffset = 0;
for (const func of scriptCoverage.functions) {
maximumEndOffset = Math.max(maximumEndOffset, ...func.ranges.map(r => r.endOffset));
}
// We only care about unused ranges of the script, so we can ignore all the nesting and safely
// assume that if a range is unexecuted, all nested ranges within it will also be unexecuted.
const unusedByIndex = new Uint8Array(maximumEndOffset);
for (const func of scriptCoverage.functions) {
for (const range of func.ranges) {
if (range.count === 0) {
for (let i = range.startOffset; i < range.endOffset; i++) {
unusedByIndex[i] = 1;
}
}
}
}
let unused = 0;
for (const x of unusedByIndex) {
unused += x;
}
return {
unusedByIndex,
unusedLength: unused,
contentLength: maximumEndOffset,
};
}
/**
* @param {string} scriptId
* @param {WasteData} wasteData
* @return {Summary}
*/
static createItem(scriptId, wasteData) {
const wastedRatio = (wasteData.unusedLength / wasteData.contentLength) || 0;
const wastedBytes = Math.round(wasteData.contentLength * wastedRatio);
return {
scriptId,
totalBytes: wasteData.contentLength,
wastedBytes,
wastedPercent: 100 * wastedRatio,
};
}
/**
* @param {WasteData} wasteData
* @param {LH.Artifacts.Bundle} bundle
*/
static createSourceWastedBytes(wasteData, bundle) {
if (!bundle.script.content) return;
/** @type {Record<string, number>} */
const files = {};
const lineLengths = bundle.script.content.split('\n').map(l => l.length);
let totalSoFar = 0;
const lineOffsets = lineLengths.map(len => {
const retVal = totalSoFar;
totalSoFar += len + 1;
return retVal;
});
// @ts-expect-error: We will upstream computeLastGeneratedColumns to CDT eventually.
bundle.map.computeLastGeneratedColumns();
for (const mapping of bundle.map.mappings()) {
let offset = lineOffsets[mapping.lineNumber];
offset += mapping.columnNumber;
const lastColumnOfMapping = mapping.lastColumnNumber !== undefined ?
mapping.lastColumnNumber - 1 :
lineLengths[mapping.lineNumber];
for (let i = mapping.columnNumber; i <= lastColumnOfMapping; i++) {
if (wasteData.unusedByIndex[offset] === 1) {
const key = mapping.sourceURL || '(unmapped)';
files[key] = (files[key] || 0) + 1;
}
offset += 1;
}
}
const dataSorted = Object.entries(files)
.sort(([_, unusedBytes1], [__, unusedBytes2]) => unusedBytes2 - unusedBytes1);
/** @type {Record<string, number>} */
const bundleData = {};
for (const [key, unusedBytes] of dataSorted) {
bundleData[key] = unusedBytes;
}
return bundleData;
}
/**
* @param {ComputeInput} data
* @return {Promise<Summary>}
*/
static async compute_(data) {
const {scriptId, scriptCoverage, bundle} = data;
const wasteData = UnusedJavascriptSummary.computeWaste(scriptCoverage);
const item = UnusedJavascriptSummary.createItem(scriptId, wasteData);
if (!bundle) return item;
return {
...item,
sourcesWastedBytes: UnusedJavascriptSummary.createSourceWastedBytes(wasteData, bundle),
};
}
}
const UnusedJavascriptSummaryComputed = makeComputedArtifact(
UnusedJavascriptSummary,
['bundle', 'scriptCoverage', 'scriptId']
);
export {UnusedJavascriptSummaryComputed as UnusedJavascriptSummary};