Skip to content

Instantly share code, notes, and snippets.

@danew
Last active July 21, 2020 11:53
Show Gist options
  • Save danew/2227537bf845dbf45804bf581d9e5c6e to your computer and use it in GitHub Desktop.
Save danew/2227537bf845dbf45804bf581d9e5c6e to your computer and use it in GitHub Desktop.
Headless Chrome lighthouse with a subset of audits

This is a simple script to run a few performance audits with Chrome's Lighthouse audit tool to help verify if you are making any performance improvements while coding.

It will print out an audit if it is outside of the given threshold; in green if it's improved or red if it hasn't.

Getting started

Run npm i -D lighthouse pretty-ms chalk
Run node test

const lighthouse = require('lighthouse');
const chromeLauncher = require('chrome-launcher');
const pretty = require('pretty-ms');
const chalk = require('chalk');
const log = console.log.bind(console.log);
(async () => {
const chrome = await chromeLauncher.launch({chromeFlags: ['--headless']});
const options = {
logLevel: 'error',
output: 'html',
port: chrome.port,
onlyAudits: [
'first-contentful-paint',
'total-blocking-time',
'first-meaningful-paint'
]
};
const runnerResult = await lighthouse('https://localhost:8080', options);
const attributes = [
['first-contentful-paint', 2389.428],
['total-blocking-time', 3795.761500000066],
['first-meaningful-paint', 154633.9120000013],
];
attributes.forEach(([attribute, timeToBeat]) => {
const attr = runnerResult.lhr.audits[attribute];
const threshold = 0.02;
if (attr.numericValue < (1-threshold) * timeToBeat) {
log(attr.title, chalk.green(pretty(timeToBeat - attr.numericValue)));
} else if (attr.numericValue > (1+threshold) * timeToBeat) {
log(attr.title, chalk.red(pretty(attr.numericValue-timeToBeat)));
}
});
await chrome.kill();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment