pprof support for Node.js.
Your application will need to be using Node.js 18 or greater. This package is tested against current versions of Node.js: 18, 20, and 22.
The
pprofmodule has a native component that is compiled on installation usingnode-gyp. You will need to install the build dependencies fornode-gyp. Seenode-gyp's documentation for details on the dependencies required for your platform.The
pprofCLI can be used to view profiles collected with this module. Instructions for installing thepprofCLI can be found here.
Install pprof with npm or add to your package.json.
# Install through npm while saving to the local 'package.json'
npm install --save pprofUpdate code to collect and save a profile:
constprofile=awaitpprof.time.profile({durationMillis: 10000,// time in milliseconds for which to // collect profile.});constbuf=awaitpprof.encode(profile);fs.writeFile('wall.pb.gz',buf,(err)=>{if(err)throwerr;});
View the profile with command line
pprof:pprof -http=: wall.pb.gz
Start program from the command line:
node --require pprof app.js
A wall time profile for the job will be saved in
pprof-profile-${process.pid}.pb.gz. View the profile with command linepprof:pprof -http=: pprof-profile-${process.pid}.pb.gz
Enable heap profiling at the start of the application:
// The average number of bytes between samples.constintervalBytes=512*1024;// The maximum stack depth for samples collected.conststackDepth=64;heap.start(intervalBytes,stackDepth);
Collect heap profiles:
Collecting and saving a profile in profile.proto format:
constprofile=awaitpprof.heap.profile();constbuf=awaitpprof.encode(profile);fs.writeFile('heap.pb.gz',buf,(err)=>{if(err)throwerr;})
View the profile with command line
pprof.pprof -http=: heap.pb.gz
Collecting a heap profile with V8 allocation profile format:
constprofile=awaitpprof.heap.v8Profile();