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

@cmmv/inspector

v0.2.0

Published

Implementation of inspector for CMMV

Downloads

16

Readme

Description

The @cmmv/inspector module provides tools for runtime performance profiling and debugging for Node.js applications. It utilizes the node:inspector module to capture CPU profiles and can be seamlessly integrated with CMMV applications to monitor and optimize performance. The module also provides utility methods to manage and persist profiling data.

Installation

Install the @cmmv/inspector package via npm:

$ pnpm add @cmmv/inspector

Quick Start

Below is a simple example of how to start, stop, and save a profile using the Inspector class:

import { Inspector } from "@cmmv/inspector";

// Start the profiler
await Inspector.start();

// Perform some operations...

// Stop the profiler and save the profile
await Inspector.stop();
await Inspector.saveProfile("/path/to/save/profile");

Process Signals

To ensure the profiler stops gracefully during application shutdown:

Inspector.bindKillProcess();

API Reference

start(): Promise<void> Starts the profiler and initializes the session.

stop(): Promise<void> Stops the profiler, captures the CPU profile, and disconnects the session.

saveProfile(dirName: string, restart: boolean = true): Promise<void> Saves the CPU profile to the specified directory. If restart is true, the profiler restarts after saving.

bindKillProcess(): void Binds to process kill signals and ensures the profiler stops gracefully.

Example Workflow

The following workflow demonstrates how to use the @cmmv/inspector module to start profiling, register cleanup tasks using the once method, perform operations, capture a heap snapshot, and gracefully stop the profiler during process termination. The once method ensures that custom finalization logic is executed before the system exits, providing a structured way to handle cleanup tasks.

import { Inspector } from "@cmmv/inspector";

async function main() {
    // Register a cleanup task
    Inspector.once(async () => {
        console.log("Performing cleanup: Saving heap snapshot...");
        await Inspector.takeHeapSnapshot("./snapshots");
        console.log("Heap snapshot saved!");
    });

    // Bind process kill signals to ensure proper finalization
    Inspector.bindKillProcess();

    // Start the profiler
    await Inspector.start();

    // Perform operations to simulate workload
    for (let i = 0; i < 1e6; i++) {
        Math.sqrt(i); // Example operation
    }

    // Stop the profiler and save the CPU profile
    await Inspector.stop();
    await Inspector.saveProfile("./profiles", false);

    console.log("Profile saved and profiler stopped!");
}

main();

Heap Snapshot

The @cmmv/inspector module includes the takeHeapSnapshot method, which captures a snapshot of the current memory heap. This feature is particularly useful for diagnosing memory leaks, analyzing object allocations, and optimizing memory usage in Node.js applications.

  • Comprehensive Memory Dump: Captures a full representation of the memory heap, including objects, closures, and references.
  • Integration with Chrome DevTools: The snapshot can be saved in .heapsnapshot format and analyzed using Chrome DevTools for detailed insights.
  • Process Monitoring: Ideal for capturing memory state during critical events, such as process termination or unexpected behavior.

Example

Below is an example demonstrating how to capture and save a heap snapshot using the takeHeapSnapshot method:

import { Inspector } from "@cmmv/inspector";

async function captureHeap() {
    const snapshotDir = "./heap_snapshots";

    console.log("Taking a heap snapshot...");
    await Inspector.takeHeapSnapshot(snapshotDir);
    console.log(`Heap snapshot saved to ${snapshotDir}`);
}

captureHeap();

Use Case in Cleanup

The takeHeapSnapshot method is especially powerful when combined with the once method to capture memory state during process termination:

Inspector.once(async () => {
    console.log("Finalizing: Capturing heap snapshot...");
    await Inspector.takeHeapSnapshot("./final_snapshots");
    console.log("Heap snapshot captured during cleanup.");
});

Benefits of Heap Snapshots

  1. Memory Leak Detection: Identify objects that are not properly garbage collected.
  2. Performance Optimization: Analyze memory usage patterns and optimize object lifetimes.
  3. Debugging: Inspect memory states to understand runtime behaviors during errors or unexpected terminations.

By leveraging the takeHeapSnapshot method, developers can gain deep visibility into application memory, helping to maintain performance and reliability.