JSPM

hw-perf-counters

1.0.0
    • ESM via JSPM
    • ES Module Entrypoint
    • Export Map
    • Keywords
    • License
    • Repository URL
    • TypeScript Types
    • README
    • Created
    • Published
    • Downloads 1
    • Score
      100M100P100Q23880F
    • License MIT

    Hardware Performance Counters for macOS

    Package Exports

    • hw-perf-counters

    Readme

    Hardware performance counters for macOS

    This package exposes macOS hardware performance counters to JavaScript.

    The code is based on this gist.

    This package will only run in Bun v0.0.79 or later due to depending on bun:ffi.

    Usage

    This package requires root access to run.

    import { init, run } from "hw-perf-count";
    
    // Set up the hardware performance counters.
    // This loads private macOS APIs
    // If sudo is not enabled, this likely will throw an error
    init();
    
    const { instructions, cycles, missedBranches, branches } = run(() => {
      for (let i = 0; i < 100000; i++) {
        // Do something
      }
    });
    
    console.log({ instructions, cycles, missedBranches, branches });
    import { init, start, stop, count } from "hw-perf-count";
    
    // Start counting
    start();
    
    // Stop counting
    stop();
    
    // How many instructions ran between start() and stop()?
    console.log(count.instructions);
    
    // How many cycles ran between start() and stop()?
    console.log(count.cycles);
    
    // How many branches missed between start() and stop()?
    console.log(count.missedBranches);
    
    // How many branches overall between start() and stop()?
    console.log(count.branches);

    count updates when you call stop().

    export const count: {
      get cycles(): number | BigInt;
      get branches(): number | BigInt;
      get instructions(): number | BigInt;
      get missedBranches(): number | BigInt;
    };