Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@siakaramalegos
Last active June 29, 2021 23:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save siakaramalegos/7cc8c0a790f8dfe0592c77cbca8440fa to your computer and use it in GitHub Desktop.
Save siakaramalegos/7cc8c0a790f8dfe0592c77cbca8440fa to your computer and use it in GitHub Desktop.
Custom metric for getting LCP image details
function getLcpElement() {
return new Promise((resolve) => {
new PerformanceObserver((entryList) => {
const lcpCandidates = entryList.getEntries();
const naiveLcpEntry = lcpCandidates[lcpCandidates.length - 1];
resolve(naiveLcpEntry);
}).observe({ type: "largest-contentful-paint", buffered: true });
}).then(({ startTime, element, url, size, loadTime, renderTime }) => {
let attributes = [];
for (let index = 0; index < element.attributes.length; index++) {
const ele = element.attributes.item(index);
attributes[index] = { name: ele.name, value: ele.value };
}
return {
startTime,
nodeName: element.nodeName,
url,
size,
loadTime,
renderTime,
attributes,
};
});
}
getLcpElement().then((res) => console.log(res));
//{
// "startTime": 202.899,
// "nodeName": "IMG",
// "url": "https://res.cloudinary.com/siacodes/image/upload/q_auto,f_auto,w_928/v1607719366/sia.codes/A_possum_and_a_movie_camera_1943_f4yflt.jpg",
// "size": 634068,
// "loadTime": 198.5,
// "renderTime": 202.899,
// "attributes": [
// {
// "name": "src",
// "value": "https://res.cloudinary.com/siacodes/image/upload/q_auto,f_auto,w_928/v1607719366/sia.codes/A_possum_and_a_movie_camera_1943_f4yflt.jpg"
// },
// {
// "name": "alt",
// "value": ""
// },
// {
// "name": "loading",
// "value": "lazy"
// }
// ]
// }
@Nithanaroy
Copy link

Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment