Skip to content

Instantly share code, notes, and snippets.

@dlockhart
Last active May 15, 2018 14:01
Show Gist options
  • Save dlockhart/9296d4d1d639c7044ac5dfc7ee4d0766 to your computer and use it in GitHub Desktop.
Save dlockhart/9296d4d1d639c7044ac5dfc7ee4d0766 to your computer and use it in GitHub Desktop.
Web Performance Presentation Notes

Web Performance Presentation Notes

Resources

Automation Tools

Code Samples

Long Tasks

const observer = new PerformanceObserver((entries) => {
    entries.getEntries().forEach((entry) => {
		const longTask = {
			startTime: entry.startTime,
			duration: entry.duration,
			attribution: JSON.stringify(entry.attribution)
		};
        sendToAnalytics('Long Task', longTask);
    });
});

observer.observe({entryTypes: ['longtask']});

First Paint & First Meaningful Paint

const observer = new PerformanceObserver((entries) => {
    entries.getEntries().forEach((entry) => {
		// name will be 'first-paint' or 'first-contentful-paint'
		const name = entry.name;
		const paintTiming = {
			name: name,
			startTime: entry.startTime,
			duration: entry.duration
		};
        sendToAnalytics('Paint', paintTiming);
    });
});

observer.observe({entryTypes: ['paint']});

Time to Interactive (TTI)

More information on the Polyfill

import ttiPolyfill from './path/to/tti-polyfill.js';

ttiPolyfill.getFirstConsistentlyInteractive().then((tti) => {
	sendToAnalytics('TTI', { value: tti });
});

First Input Delay (TTI)

More information on the Script

// add the minified script from https://github.com/GoogleChromeLabs/first-input-delay into the <head>

perfMetrics.onFirstInputDelay(function(delay, evt) {
	sendToAnalytics('first-input-delay', { value: Math.round(delay) });
});

Input Latency

const someButton = document.getElementById('some-button');
someButton.addEventListener('click', (event) => {
	// handle click event here
	const lag = performance.now() - event.timeStamp;
	if (lag > 100) {
		sendToAnalytics('input-latency', {
			elem: '#some-button',
			value: lag
		});
	}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment