Skip to content

Commit

Permalink
core(unused-css): exclude header size for estimating wasted bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjclark committed Dec 8, 2023
1 parent 2338efc commit 67ba167
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
8 changes: 4 additions & 4 deletions core/computed/unused-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import {makeComputedArtifact} from './computed-artifact.js';
import {NetworkRecords} from './network-records.js';
import {Util} from '../../shared/util.js';
import {estimateTransferSize} from '../lib/script-helpers.js';
import {estimateCompressedContentSize} from '../lib/script-helpers.js';

const PREVIEW_LENGTH = 100;

Expand Down Expand Up @@ -70,15 +70,15 @@ class UnusedCSS {
usedUncompressedBytes += usedRule.endOffset - usedRule.startOffset;
}

const totalTransferredBytes = estimateTransferSize(
const compressedSize = estimateCompressedContentSize(
stylesheetInfo.networkRecord, totalUncompressedBytes, 'Stylesheet');
const percentUnused = (totalUncompressedBytes - usedUncompressedBytes) / totalUncompressedBytes;
const wastedBytes = Math.round(percentUnused * totalTransferredBytes);
const wastedBytes = Math.round(percentUnused * compressedSize);

return {
wastedBytes,
wastedPercent: percentUnused * 100,
totalBytes: totalTransferredBytes,
totalBytes: compressedSize,
};
}

Expand Down
1 change: 1 addition & 0 deletions core/test/audits/byte-efficiency/unused-css-rules-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ describe('Best Practices: unused css rules audit', () => {
transferSize: 100 * 1024 * 0.5, // compression ratio of 0.5
resourceSize: 100 * 1024,
resourceType: 'Document', // this is a document so it'll use the compressionRatio but not the raw size
responseHeaders: [{name: 'Content-Encoding'}],
},
{
url: 'file://a.html',
Expand Down
3 changes: 1 addition & 2 deletions core/test/network-records-to-devtools-log.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,7 @@ function getResponseReceivedEvent(networkRecord, index, normalizedTiming) {
connectionId: networkRecord.connectionId || 140,
fromDiskCache: networkRecord.fromDiskCache || false,
fromServiceWorker: networkRecord.fetchedViaServiceWorker || false,
encodedDataLength:
networkRecord.responseHeadersTransferSize || networkRecord.transferSize || 0,
encodedDataLength: networkRecord.responseHeadersTransferSize || 0,
timing: {...normalizedTiming.timing},
protocol: networkRecord.protocol || 'http/1.1',
},
Expand Down

0 comments on commit 67ba167

Please sign in to comment.