From e9410a15ea3c3199588d8ee0c24b0fb68e05334a Mon Sep 17 00:00:00 2001 From: Paul Irish Date: Fri, 11 Oct 2019 08:42:51 -0700 Subject: [PATCH] core(image-elements): cache naturalSize results (#9818) --- lighthouse-core/gather/gatherers/image-elements.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lighthouse-core/gather/gatherers/image-elements.js b/lighthouse-core/gather/gatherers/image-elements.js index 77b7e566db50..f4923e7f5033 100644 --- a/lighthouse-core/gather/gatherers/image-elements.js +++ b/lighthouse-core/gather/gatherers/image-elements.js @@ -130,6 +130,12 @@ function determineNaturalSize(url) { } class ImageElements extends Gatherer { + constructor() { + super(); + /** @type {Map} */ + this._naturalSizeCache = new Map(); + } + /** * @param {Driver} driver * @param {LH.Artifacts.ImageElement} element @@ -137,11 +143,16 @@ class ImageElements extends Gatherer { */ async fetchElementWithSizeInformation(driver, element) { const url = JSON.stringify(element.src); + if (this._naturalSizeCache.has(url)) { + return Object.assign(element, this._naturalSizeCache.get(url)); + } + try { // We don't want this to take forever, 250ms should be enough for images that are cached driver.setNextProtocolTimeout(250); /** @type {{naturalWidth: number, naturalHeight: number}} */ const size = await driver.evaluateAsync(`(${determineNaturalSize.toString()})(${url})`); + this._naturalSizeCache.set(url, size); return Object.assign(element, size); } catch (_) { // determineNaturalSize fails on invalid images, which we treat as non-visible