Skip to content

Commit f3dc6b1

Browse files
Performance.measure custom track
1 parent 8fdb142 commit f3dc6b1

File tree

1 file changed

+68
-3
lines changed

1 file changed

+68
-3
lines changed

photo-gallery/gallery.js

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const galleryEl = document.querySelector(".gallery");
55
const filterEl = document.querySelector(".filters");
66
const filesEl = document.querySelector('.files');
77

8+
const customPerformanceTrackGroupName = "Custom performance timings";
9+
810
function formatDate(data) {
911
const date = new Date(data.DateTime.substring(0, 10).replace(/:/g, '/'));
1012
return date.toLocaleDateString();
@@ -175,6 +177,8 @@ function populateGallery(images) {
175177
galleryEl.innerHTML = '';
176178

177179
images.forEach(({ file, user, description, w, h, meta }) => {
180+
const imageCreationStart = performance.now();
181+
178182
const liEl = document.createElement("li");
179183
liEl.classList.add('photo');
180184
liEl.setAttribute('tabindex', '0');
@@ -215,12 +219,12 @@ function populateGallery(images) {
215219

216220
const beforeShadowEl = document.createElement("div");
217221
beforeShadowEl.classList.add('before-shadow');
218-
beforeShadowEl.style = `width:${w}px;left:${(300 - w)/2}px;`;
222+
beforeShadowEl.style = `width:${w}px;left:${(300 - w) / 2}px;`;
219223
photoWrapperEl.appendChild(beforeShadowEl);
220-
224+
221225
const afterShadowEl = document.createElement("div");
222226
afterShadowEl.classList.add('after-shadow');
223-
afterShadowEl.style = `width:${w}px;left:${(300 - w)/2}px;bottom:0;`;
227+
afterShadowEl.style = `width:${w}px;left:${(300 - w) / 2}px;bottom:0;`;
224228
photoWrapperEl.appendChild(afterShadowEl);
225229

226230
const imageEl = document.createElement("img");
@@ -287,6 +291,27 @@ function populateGallery(images) {
287291
metaEl.querySelector('.exposure strong').textContent = formatExposureTime(meta);
288292
metaEl.querySelector('.focal-length strong').textContent = formatFocalLength(meta);
289293
metaEl.querySelector('.iso strong').textContent = formatISO(meta);
294+
295+
const perfMeasureDescription = `Image ${file} created`;
296+
performance.measure(perfMeasureDescription, {
297+
start: imageCreationStart,
298+
end: performance.now(),
299+
detail: {
300+
devtools: {
301+
dataType: "track-entry",
302+
color: "primary",
303+
trackGroup: customPerformanceTrackGroupName,
304+
track: "Photo creation",
305+
properties: [
306+
['File', file],
307+
['Width', w],
308+
['Height', h],
309+
['User', user],
310+
],
311+
tooltipText: perfMeasureDescription
312+
}
313+
},
314+
});
290315
});
291316
}
292317

@@ -301,6 +326,8 @@ getImageData().then(imageData => {
301326
addEventListener('input', e => {
302327
const filter = e.target.closest('.filter select');
303328
if (filter) {
329+
const filterStartTime = performance.now();
330+
304331
// Reset the other filters
305332
filterEl.querySelectorAll('.filter select').forEach(select => {
306333
if (select !== filter) {
@@ -325,6 +352,24 @@ addEventListener('input', e => {
325352
filterByISO(filter.value);
326353
break;
327354
}
355+
356+
const description = `Filter applied: ${filter.id}`;
357+
performance.measure(description, {
358+
start: filterStartTime,
359+
end: performance.now(),
360+
detail: {
361+
devtools: {
362+
dataType: "track-entry",
363+
color: "secondary",
364+
trackGroup: customPerformanceTrackGroupName,
365+
track: "Filtering",
366+
properties: [
367+
['Filter Value', filter.value]
368+
],
369+
tooltipText: description
370+
}
371+
},
372+
});
328373
}
329374
});
330375

@@ -366,10 +411,30 @@ addEventListener('keydown', e => {
366411
});
367412

368413
function loadPhoto(fileName) {
414+
const loadStartTime = performance.now();
415+
const perfMeasureDescription = `Loading photo: ${fileName}`;
416+
369417
return new Promise(resolve => {
370418
const imageEl = document.createElement("img");
371419
imageEl.src = fileName;
372420
imageEl.addEventListener('load', () => {
421+
performance.measure(perfMeasureDescription, {
422+
start: loadStartTime,
423+
end: performance.now(),
424+
detail: {
425+
devtools: {
426+
dataType: "track-entry",
427+
color: "tertiary",
428+
trackGroup: customPerformanceTrackGroupName,
429+
track: "Loading",
430+
properties: [
431+
['Photo', fileName]
432+
],
433+
tooltipText: perfMeasureDescription
434+
}
435+
},
436+
});
437+
373438
resolve(imageEl);
374439
}, { once: true });
375440
});

0 commit comments

Comments
 (0)