UNPKG

sitespeed.io

Version:

sitespeed.io is an open-source tool for comprehensive web performance analysis, enabling you to test, monitor, and optimize your website’s speed using real browsers in various environments.

43 lines (38 loc) 1.17 kB
export class AssetsBySize { constructor(maxSize) { this.maxSize = maxSize; this.items = []; this.smallest = 0; } add(asset, page, runPage) { if (asset.contentSize > this.smallest || this.items.length < this.maxSize) { // check that the URL doesn't already exists.... if (!this.items.some(assetInArray => assetInArray.url === asset.url)) { this.items.push({ url: asset.url, contentSize: asset.contentSize, transferSize: asset.transferSize, lastModification: asset.timeSinceLastModified, cacheTime: asset.expires, page, runPage }); } if (this.items.length > this.maxSize) { this.items.sort(function (asset, asset2) { return asset2.contentSize - asset.contentSize; }); if (this.items.length > this.maxSize) { this.items.length = this.maxSize; this.smallest = this.items[this.maxSize - 1].contentSize; } } } } getItems() { this.items.sort(function (asset, asset2) { return asset2.contentSize - asset.contentSize; }); return this.items; } }