Menu

Build a Lightweight Page Speed Analyzer with Lighthouse and Node.js

Build a Lightweight Page Speed Analyzer with Lighthouse and Node.js

Sometimes, you just want to know how fast your page really is, without waiting for full dashboards to load or logging into anything. Here’s a lightweight way to do that using Google’s Lighthouse CLI, right from a Node.js script.

This small utility lets you programmatically measure performance, SEO, accessibility, and more. You can run it manually, automate it, or even wrap it in an API later if you need something more robust.

Install the Libraries

To get started, install Lighthouse and Chrome Launcher:

npm install lighthouse chrome-launcher

This gives you everything needed to launch a headless browser and run audits without leaving your codebase.

The Code

Here’s a minimal script that launches Chrome, runs a Lighthouse audit on a URL, and logs the performance score:

app.get('/lighthouse', async (req, res) => {
  const url = req.query.url || 'https://www.thatsoftwaredude.com';
  const { launch } = await import('chrome-launcher');
  const lighthouse = await import('lighthouse');

  const chrome = await launch({ chromeFlags: ['--headless'] });
  const options = {
    logLevel: 'info',
    output: 'json',
    onlyCategories: ['performance'],
    port: chrome.port,
  };
  
  const runnerResult = await lighthouse.default(url, options);

  // save runnerResult to a file
  const fs = require('fs');
  const reportJson = JSON.stringify(runnerResult.lhr, null, 2);
  fs.writeFileSync('report.json', reportJson, 'utf8', (err) => {
    if (err) {
      console.error('Error writing report to file:', err);
    } else {
      console.log('Report saved to report.json');
    }
  });

  const performanceScore = runnerResult.lhr.categories.performance.score * 100;
  console.log(`Performance score: ${performanceScore}`);

  await chrome.kill();
});

What You Get

This will generate a full Lighthouse audit in JSON format. You can easily switch to HTML output if needed. The results include:

  • Overall performance score
  • First Contentful Paint
  • Speed Index
  • Time to Interactive

You can take it further by:

  • Sending alerts if scores drop below a threshold
  • Tracking performance trends over time

Why Bother?

Because you shouldn’t need DevTools just to know if your site is sluggish. With a simple script like this, you can benchmark page speed, run it during deployments, or keep an eye on changes, all with minimal setup.

It’s especially useful for developers running blogs, side projects, or lightweight SaaS sites where performance matters but time is limited.

Other Ways to Use Lighthouse

This guide focuses on performance, but Lighthouse offers much more. With the right setup, you can:

  • Audit for SEO, accessibility, PWA compliance, and best practices
  • Generate shareable HTML reports with one flag change
  • Switch between mobile and desktop emulation
  • Automate audits in your CI/CD pipeline
  • Monitor competitors' sites for performance trends
  • Write custom audits for your own domain-specific needs

With a little creativity, Lighthouse can go from “nice-to-have” to a fully integrated part of your workflow.

Let me know if you want to see how this could be turned into an API or dashboard next.

Walter Guevara is a Computer Scientist, software engineer, startup founder and previous mentor for a coding bootcamp. He has been creating software for the past 20 years.
AD: "Heavy scripts slowing down your site? I use Fathom Analytics because it’s lightweight, fast, and doesn’t invade my users privacy." - Get $10 OFF your first invoice.

Community Comments

No comments posted yet

Code Your Own Classic Snake Game – The Right Way

Master the fundamentals of game development and JavaScript with a step-by-step guide that skips the fluff and gets straight to the real code.

Ad Unit

Current Poll

Help us and the community figure out what the latest trends in coding are.

Total Votes:
Q:
Submit

Add a comment