npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

perf-observer-js

v2.2.3

Published

A library for monitoring performance metrics using Service Workers

Downloads

18

Readme

Performance Observer JS

npm version License: MIT Bundle Size Tests Downloads GitHub stars

A lightweight, zero-dependency library for monitoring network performance in web applications. Built with TypeScript, it provides detailed insights into your application's network requests, including timing metrics and response headers.

Features

  • 🔍 Comprehensive Network Monitoring: Track all network requests with detailed timing information
  • 📊 Response Headers: Access response headers for each request
  • Service Worker Based: Uses Service Workers for reliable request interception
  • 🔄 Transform Support: Customize performance entries with transform functions
  • 🎯 TypeScript Ready: Full TypeScript support with type definitions
  • 📦 Zero Dependencies: Lightweight and framework-agnostic
  • 🛡️ Error Handling: Robust error handling for failed requests

Installation

npm install performance-observer-js
# or
yarn add performance-observer-js

Quick Start

import { PerformanceMonitor } from 'performance-observer-js';

// Initialize the monitor
const monitor = new PerformanceMonitor({
  workerUrl: '/worker.js'
});

// Subscribe to performance entries
const subscription = monitor.subscribe((entry) => {
  console.log('Network request:', entry);
  // Access timing information
  console.log('Request duration:', entry.duration);
  // Access response headers
  console.log('Response headers:', entry.responseHeaders);
});

// Cleanup when done
subscription.unsubscribe();
// or
monitor.disconnect();

Configuration

The PerformanceMonitor constructor accepts the following configuration options:

interface PerformanceMonitorConfig {
  workerUrl: string;  // Required: URL to the service worker file
  transform?: (entry: PerformanceEntryWithHeaders) => PerformanceEntryWithHeaders;  // Optional: Transform function
}

Example with Transform

const monitor = new PerformanceMonitor({
  workerUrl: '/worker.js',
  transform: (entry) => {
    // Add custom properties
    return {
      ...entry,
      customMetric: entry.duration * 2
    };
  }
});

Performance Entry Structure

Each performance entry includes:

interface PerformanceEntryWithHeaders {
  name: string;              // Request URL
  entryType: string;         // Always 'resource'
  startTime: number;         // Request start time
  duration: number;          // Total request duration
  responseHeaders: {         // Response headers
    [key: string]: string;
  };
  timing: {                  // Detailed timing information
    connectStart: number;
    connectEnd: number;
    domainLookupStart: number;
    domainLookupEnd: number;
    fetchStart: number;
    requestStart: number;
    responseStart: number;
    responseEnd: number;
    secureConnectionStart: number;
    redirectStart: number;
    redirectEnd: number;
  } | null;
  request: {                 // Request details
    method: string;
    type: string;
  };
  error?: string;            // Error message if request failed
}

Setup

1. Copy Worker File

First, copy the worker file from the package to your public directory:

cp node_modules/performance-observer-js/public/worker.js public/

2. Configure Your Build Tool

Webpack

// webpack.config.js
module.exports = {
  // ... other config
  output: {
    publicPath: '/'
  }
};

Vite

// vite.config.js
export default {
  // ... other config
  publicDir: 'public'
};

Next.js

// next.config.js
module.exports = {
  // ... other config
  async rewrites() {
    return [
      {
        source: '/worker.js',
        destination: '/public/worker.js'
      }
    ];
  }
};

3. Manual Setup

If you're not using a build tool, ensure your web server is configured to serve the worker file from the public directory.

Browser Support

  • Chrome 60+
  • Firefox 54+
  • Safari 11.1+
  • Edge 79+

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments