From 446be449d97c534405e3c4df6a687aee281f12c6 Mon Sep 17 00:00:00 2001 From: Paul Irish Date: Thu, 10 Oct 2019 17:08:06 -0700 Subject: [PATCH 1/3] core(image-elements): cache naturalSize results --- lighthouse-core/gather/gatherers/image-elements.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lighthouse-core/gather/gatherers/image-elements.js b/lighthouse-core/gather/gatherers/image-elements.js index 77b7e566db50..91557257304b 100644 --- a/lighthouse-core/gather/gatherers/image-elements.js +++ b/lighthouse-core/gather/gatherers/image-elements.js @@ -130,6 +130,11 @@ 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 +142,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 From 86048efc9d71c1c23a48cb17c219ecf4882cc379 Mon Sep 17 00:00:00 2001 From: Paul Irish Date: Thu, 10 Oct 2019 19:17:06 -0700 Subject: [PATCH 2/3] Update lighthouse-core/gather/gatherers/image-elements.js Co-Authored-By: Patrick Hulce --- lighthouse-core/gather/gatherers/image-elements.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lighthouse-core/gather/gatherers/image-elements.js b/lighthouse-core/gather/gatherers/image-elements.js index 91557257304b..fdb01d655087 100644 --- a/lighthouse-core/gather/gatherers/image-elements.js +++ b/lighthouse-core/gather/gatherers/image-elements.js @@ -135,6 +135,7 @@ class ImageElements extends Gatherer { /** @type {Map} */ this._naturalSizeCache = new Map(); } + /** * @param {Driver} driver * @param {LH.Artifacts.ImageElement} element From 2d015487b33370839bd4f0c9fed9590cb8ac8830 Mon Sep 17 00:00:00 2001 From: Paul Irish Date: Thu, 10 Oct 2019 21:21:27 -0700 Subject: [PATCH 3/3] lint --- lighthouse-core/gather/gatherers/image-elements.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lighthouse-core/gather/gatherers/image-elements.js b/lighthouse-core/gather/gatherers/image-elements.js index fdb01d655087..f4923e7f5033 100644 --- a/lighthouse-core/gather/gatherers/image-elements.js +++ b/lighthouse-core/gather/gatherers/image-elements.js @@ -135,7 +135,7 @@ class ImageElements extends Gatherer { /** @type {Map} */ this._naturalSizeCache = new Map(); } - + /** * @param {Driver} driver * @param {LH.Artifacts.ImageElement} element